Skip to content

Instantly share code, notes, and snippets.

@mangelsnc
Created August 16, 2018 21:26
Adapter Pattern - Before
<?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');
}
}
<?php declare(strict_types = 1);
class DatabaseLogger()
{
private $database;
//Initialization code
// ...
// ...
public function insertRecord(string $string)
{
$this->database->insert($string);
}
}
<?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);
}
}
<?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