2021-07-15 21:17:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Pdf;
|
|
|
|
|
|
|
|
use App\Member\Member;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class PdfRepositoryFactory
|
|
|
|
{
|
|
|
|
|
2021-07-17 16:57:37 +02:00
|
|
|
private array $types = [
|
|
|
|
BillType::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getTypes(): Collection
|
|
|
|
{
|
|
|
|
return collect($this->types);
|
|
|
|
}
|
|
|
|
|
2021-07-15 21:17:48 +02:00
|
|
|
public function fromSingleRequest(string $type, Member $member): ?PdfRepository
|
|
|
|
{
|
2021-07-17 18:10:13 +02:00
|
|
|
$members = $this->singleMemberCollection($member, $type);
|
2021-07-15 21:17:48 +02:00
|
|
|
|
|
|
|
if ($members->isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$repo = $this->resolve($type, $members);
|
|
|
|
$firstMember = $members->first()->first();
|
|
|
|
|
|
|
|
return $repo->setFilename(
|
2021-07-17 17:29:59 +02:00
|
|
|
Str::slug("{$repo->getSubject()} für {$firstMember->lastname}"),
|
2021-07-15 21:17:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-17 18:10:13 +02:00
|
|
|
public function singleMemberCollection(Member $member, string $type): Collection
|
2021-07-15 21:17:48 +02:00
|
|
|
{
|
2021-07-17 18:10:13 +02:00
|
|
|
$members = Member::where($member->only(['lastname', 'address', 'zip', 'location']))
|
|
|
|
->get()
|
|
|
|
->filter(fn (Member $member) => app($type)->createable($member));
|
2021-07-15 21:17:48 +02:00
|
|
|
|
|
|
|
return $this->toMemberGroup($members);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolve(string $kind, Collection $members): PdfRepository
|
|
|
|
{
|
|
|
|
return new $kind($members);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function toMemberGroup(Collection $members): Collection
|
|
|
|
{
|
|
|
|
return $members->groupBy(
|
|
|
|
fn ($member) => Str::slug(
|
2021-07-17 18:10:13 +02:00
|
|
|
"{$member->lastname}{$member->address}{$member->zip}{$member->location}",
|
2021-07-15 21:17:48 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|