/AwesomeService.php Secret
Created
August 16, 2018 21:26
Adapter Pattern - Before
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
class AwesomeService | |
{ | |
public function doSomethingCool(FileLogger $logger) | |
{ | |
$logger->addLine("I'm gonna make something cool..."); | |
// Make awesome things | |
// ... | |
// ... | |
$logger->addLine('Something cool was done'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
class DatabaseLogger() | |
{ | |
private $database; | |
//Initialization code | |
// ... | |
// ... | |
public function insertRecord(string $string) | |
{ | |
$this->database->insert($string); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
class FileLogger | |
{ | |
private $file; | |
public function __construct(string $filePath) | |
{ | |
if (!file_exists($filePath) || !is_file($filePath)) { | |
throw new InvalidFilePathException(); | |
} | |
$this->file = fopen($filePath, 'a'); | |
} | |
public function __destruct() | |
{ | |
fclose($this->file); | |
} | |
public function addLine(string $line) | |
{ | |
fwrite($this->logFile, $line); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
class NetworkLogger() | |
{ | |
private $client; | |
//Initialization code | |
// ... | |
// ... | |
public function sendLog(LogLine $line) | |
{ | |
$this->client->send($line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment