Skip to content

Instantly share code, notes, and snippets.

@l3l0
Last active December 23, 2018 12:00
Show Gist options
  • Select an option

  • Save l3l0/edcc9b84aed8ece61e0bbce8d6b2d0a4 to your computer and use it in GitHub Desktop.

Select an option

Save l3l0/edcc9b84aed8ece61e0bbce8d6b2d0a4 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
class AddressDepartamentContentQuery implements ContentQuery
{
private $addressRepository;
private $departamentRepository;
public function __consturct(AddressRepository $addressRepository, DepartamentRepository $departamentRepository)
{
$this->addressRepository = $addressRepository;
$this->departamentRepository = $departamentRepository;
}
public function fetch(): array
{
$addresses = $this->addressRepository->findAll();
$departments = $this->departmentRepository->findAll();
$pdfInformation = [];
foreach ($addresses as $i => $address) {
if ($address->withDepartments()) {
$pdfInformation[$i]['address'] = $address->getFullAddress();
$pdfInformation[$i]['person'] = $address->getPerson();
} else {
foreach ($departments as $department) {
$pdfInformation[$i][$department->getName()]['address'] = $address->getFullAddress();
if ($address->getPerson()) {
$pdfInformation[$i][$department->getName()]['person'] = $address->getPerson();
} else {
$pdfInformation[$i][$department->getName()]['contact'] = $this->sanitize($address->getContact());
}
}
}
}
return $pdfInformation;
}
}
<?php
interface ContentQuery
{
public function fetch(): array;
}
<?php
declare(strict_types=1);
final class FPDFWriter implements PDFWriter
{
private $pdf;
private $webFolder;
public function __construct(FPDF $fpdf, string $webFolder)
{
$this->pdf = $fpdf;
$this->webFolder = $webFolder;
}
public function writer(array $content): void
{
$pdf = $this->fpdf->generate($content);
file_put_contents($this->webFolder . $pdf->getUri());
}
}
<?php
declare(strict_types=1);
class PDFGenerator
{
private $contentQuery;
private $pdfWriter;
public function __construct(PDFContentQuery $contentQuery, PDFWriter $writer)
{
$this->contentQuery = $contentQuery;
$this->pdfWriter = $writer;
}
public function execute(): void
{
$this->pdfWriter->write($this->contentQuery->fetch());
}
}
<?php
interface PDFWriter
{
public function write(array $content): void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment