adrema/app/Invoice/DocumentFactory.php

68 lines
1.5 KiB
PHP
Raw Normal View History

2022-11-07 16:18:11 +01:00
<?php
2023-04-18 22:08:45 +02:00
namespace App\Invoice;
2022-11-07 16:18:11 +01:00
2023-04-18 22:08:45 +02:00
use App\Invoice\Queries\InvoiceMemberQuery;
2022-11-07 16:18:11 +01:00
use Illuminate\Support\Collection;
class DocumentFactory
{
/**
2023-04-18 22:08:45 +02:00
* @var array<int, class-string<Invoice>>
2022-11-07 16:18:11 +01:00
*/
2022-12-07 00:40:53 +01:00
private array $types = [
2022-11-07 16:18:11 +01:00
BillDocument::class,
RememberDocument::class,
];
/**
2023-04-18 22:08:45 +02:00
* @return Collection<int, class-string<Invoice>>
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
}
/**
2023-04-18 22:08:45 +02:00
* @param class-string<Invoice> $type
2022-11-07 16:18:11 +01:00
*/
2023-04-18 22:08:45 +02:00
public function singleInvoice(string $type, InvoiceMemberQuery $query): ?Invoice
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
}
/**
2023-04-18 22:08:45 +02:00
* @param class-string<Invoice> $type
2022-11-07 16:18:11 +01:00
*
2023-04-18 22:08:45 +02:00
* @return Collection<int, Invoice>
2022-11-07 16:18:11 +01:00
*/
2023-04-18 22:08:45 +02:00
public function invoiceCollection(string $type, InvoiceMemberQuery $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
}
2023-04-18 22:08:45 +02:00
public function afterSingle(Invoice $invoice): void
2022-11-07 16:18:11 +01:00
{
2023-04-18 22:08:45 +02:00
foreach ($invoice->allPayments() as $payment) {
$invoice->afterSingle($payment);
2022-11-07 16:18:11 +01:00
}
}
/**
2023-04-18 22:08:45 +02:00
* @param class-string<Invoice> $type
2022-11-07 16:18:11 +01:00
* @param Collection<int, Page> $pages
*/
2023-04-18 22:08:45 +02:00
private function resolve(string $type, Collection $pages): Invoice
2022-11-07 16:18:11 +01:00
{
return new $type($pages);
}
}