Skip to content

Instantly share code, notes, and snippets.

@adrianpietka
Last active November 16, 2016 13:59
Show Gist options
  • Select an option

  • Save adrianpietka/13564bb44157ecc72c910aff2b614c2d to your computer and use it in GitHub Desktop.

Select an option

Save adrianpietka/13564bb44157ecc72c910aff2b614c2d to your computer and use it in GitHub Desktop.
SOLID - Technical Meeting
<?php
class Document
{
public $id;
public $title;
public $created;
public $author;
public $content;
}
interface IStorage
{
public function save(IOutput $output);
}
interface IOutput
{
public function content();
}
class FilesystemStorage implements IStorage
{
public function save(IOutput $output)
{
$path = sys_get_temp_dir() . '/' . rand();
file_put_contents($path, $output->content());
return $path;
}
}
class DocumentHtmlOutput implements IOutput
{
private $document;
public function __construct(Document $document)
{
$this->document = $document;
}
public function content()
{
return '<h1>' . $this->document->title . '</h1><hr>' . $this->document->content;
}
}
class SaveHtmlDocumentToFilesystemCommand
{
public function execute($id)
{
$document = new Document();
$document->id = $id;
$output = new DocumentHtmlOutput($document);
$storage = new FilesystemStorage();
$storage->save($output);
}
}
/**
* /document/1/azure/pdf
*/
function savePdfDocumentToAzure($id)
{
new (SaveHtmlDocumentToFilesystemCommand())->execute($id);
}
<?php
class Document
{
public $title;
public $created;
public $author;
public $content;
public function save(StorageInterface $storage, FormatInterface $format)
{
return $storage->save($format->getFilename(), $format->getContent());
}
}
interface StorageInterface
{
public function save($filename, $content);
}
class FilesystemStorage implements StorageInterface
{
public function save($filename, $content)
{
$tmpPath = tempnam(sys_get_temp_dir(), $filename);
return file_put_contents($tmpPath, $content) === mb_strlen($content);
}
}
interface FormatInterface
{
public function getFilename();
public function getContent();
}
class DocumentHtmlFormat implements FormatInterface
{
private $document;
public function __construct(Document $document)
{
$this->document = $document;
}
public function getFilename()
{
return join([
$this->document->title,
time()
], '-') . '.html';
}
public function getContent()
{
$vars = [
'%title%' => $this->document->title,
'%author%' => $this->document->author,
'%created%' => $this->document->created->format('Y-m-d H:i:s'),
'%content%' => $this->document->content
];
$template = '<html><body><h1>%title%</bodyh1><p>by: %author%, %created%</p><hr>%content%</h1></body></html>';
return str_replace(array_keys($vars), array_values($vars), $template);
}
}
$document = new Document();
$document->title = 'Lorem Ipsum';
$document->created = new DateTime();
$document->author = 'Adrian Pietka';
$document->content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
$filesystemStorage = new FilesystemStorage();
$htmlFormat = new DocumentHtmlFormat($document);
var_dump($document->save($filesystemStorage, $htmlFormat));
<?php
class Document
{
public $title;
public $created;
public $author;
public $content;
}
interface StorageInterface
{
public function save($filename, $content);
}
class FilesystemStorage implements StorageInterface
{
public function save($filename, $content)
{
$tmpPath = tempnam(sys_get_temp_dir(), $filename);
return file_put_contents($tmpPath, $content) === mb_strlen($content);
}
}
interface FormatInterface
{
public function getFilename();
public function getContent();
}
class DocumentHtmlFormat implements FormatInterface
{
private $document;
public function __construct(Document $document)
{
$this->document = $document;
}
public function getFilename()
{
return join([
$this->document->title,
time()
], '-') . '.html';
}
public function getContent()
{
$vars = [
'%title%' => $this->document->title,
'%author%' => $this->document->author,
'%created%' => $this->document->created->format('Y-m-d H:i:s'),
'%content%' => $this->document->content
];
$template = '<html><body><h1>%title%</bodyh1><p>by: %author%, %created%</p><hr>%content%</h1></body></html>';
return str_replace(array_keys($vars), array_values($vars), $template);
}
}
class SaveDocumentAsHtmlOnFilesystemCommand {
public function execute(Document $document) {
$filesystemStorage = new FilesystemStorage();
$htmlFormat = new DocumentHtmlFormat($document);
var_dump($filesystemStorage->save($htmlFormat->getFilename(), $htmlFormat->getContent()));
}
}
$document = new Document();
$document->title = 'Lorem Ipsum';
$document->created = new DateTime();
$document->author = 'Adrian Pietka';
$document->content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
(new SaveDocumentAsHtmlOnFilesystemCommand)->execute($document);
<?php
class Document
{
public $title;
public $created;
public $author;
public $content;
public function saveAsPdf()
{
$filename = join([$this->document->title, time()], '-') . '.html';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, $this->title);
/// etc.
return $pdf->Output(sys_get_temp_dir(), $filename);
}
public function saveAsHtml()
{
$filename = join([$this->document->title, time()], '-') . '.html';
$content = $this->getContentOfHtml();
$tmpPath = tempnam(sys_get_temp_dir(), $filename);
return file_put_contents($tmpPath, $content) === mb_strlen($content);
}
public function savePdfToAzure()
{
$file = $this->saveAsPdf();
$azure = new AzureStorage();
// etc.
}
public function getContentOfHtml()
{
$vars = [
'%title%' => $this->title,
'%author%' => $this->author,
'%created%' => $this->created->format('Y-m-d H:i:s'),
'%content%' => $this->content
];
$template = '<html><body><h1>%title%</bodyh1><p>by: %author%, %created%</p><hr>%content%</h1></body></html>';
return str_replace(array_keys($vars), array_values($vars), $template);
}
}
<?php
class Samochod
{
public function rusz(); // samod rusza sam [true]
public function zatrzymaj(); // samochod zatrzumuje sie sam [true]
public function pobierzOlej(); // samochod pobiera olej sam [true]
}
class Kierowca
{
public function kieruj(Samochod $samochod); // samochod kieruje sie sam [false]
}
class HosstessaCycataFest implements Kształtna
{
public function myj(Samochod $samochod); // samochod myje sie sam [false]
}
class Mechanik
{
public function sprawdzOlej(Samochod $samochod); // samochod sprawdza olej sam [false]
public function zmienOpone(Samochod $samochod, Opona $opona); // samochod zmienia opone sam [false]
}
<?php
class Samochod {
public function rusz();
public function zatrzymaj();
public function pobierzOlej();
public function kieruj();
public function myj();
public function sprawdzOlej();
public function zmienOpone();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment