adrema/app/Letter/DocumentFactory.php

68 lines
1.5 KiB
PHP
Raw Normal View History

2022-11-07 16:18:11 +01:00
<?php
namespace App\Letter;
2022-12-07 00:40:53 +01:00
use App\Letter\Queries\LetterMemberQuery;
2022-11-07 16:18:11 +01:00
use Illuminate\Support\Collection;
class DocumentFactory
{
/**
* @var array<int, class-string<Letter>>
*/
2022-12-07 00:40:53 +01:00
private array $types = [
2022-11-07 16:18:11 +01:00
BillDocument::class,
RememberDocument::class,
];
/**
2022-12-07 00:40:53 +01:00
* @return Collection<int, class-string<Letter>>
2022-11-07 16:18:11 +01:00
*/
public function getTypes(): Collection
{
2022-12-07 00:40:53 +01:00
return collect($this->types);
2022-11-07 16:18:11 +01:00
}
/**
* @param class-string<Letter> $type
*/
2022-12-07 00:40:53 +01:00
public function singleLetter(string $type, LetterMemberQuery $query): ?Letter
2022-11-07 16:18:11 +01:00
{
2022-12-07 00:40:53 +01:00
$pages = $query->getPages($type);
2022-11-07 16:18:11 +01:00
2022-12-06 22:16:37 +01:00
if ($pages->isEmpty()) {
2022-11-07 16:18:11 +01:00
return null;
}
2022-12-07 00:40:53 +01:00
return $this->resolve($type, $pages);
2022-11-07 16:18:11 +01:00
}
/**
* @param class-string<Letter> $type
*
* @return Collection<int, Letter>
*/
2022-12-07 00:40:53 +01:00
public function letterCollection(string $type, LetterMemberQuery $query): Collection
2022-11-07 16:18:11 +01:00
{
2022-12-07 00:40:53 +01:00
return $query
->getPages($type)
->map(fn ($page) => $this->resolve($type, collect([$page])));
2022-11-07 16:18:11 +01:00
}
2022-12-06 22:16:37 +01:00
public function afterSingle(Letter $letter): void
2022-11-07 16:18:11 +01:00
{
2022-12-06 22:16:37 +01:00
foreach ($letter->allPayments() as $payment) {
$letter->afterSingle($payment);
2022-11-07 16:18:11 +01:00
}
}
/**
* @param class-string<Letter> $type
* @param Collection<int, Page> $pages
*/
private function resolve(string $type, Collection $pages): Letter
{
return new $type($pages);
}
}