Refactor Query of DocumentFactory
This commit is contained in:
parent
1f103b7539
commit
3a93d1585f
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace App\Letter;
|
namespace App\Letter;
|
||||||
|
|
||||||
|
use App\Letter\Queries\BillKindQuery;
|
||||||
|
use App\Letter\Queries\SingleMemberQuery;
|
||||||
use App\Member\Member;
|
use App\Member\Member;
|
||||||
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
@ -94,15 +96,7 @@ class DocumentFactory
|
||||||
*/
|
*/
|
||||||
private function singleMemberPages(Member $member, string $type): Collection
|
private function singleMemberPages(Member $member, string $type): Collection
|
||||||
{
|
{
|
||||||
$members = Member::where($member->only(['lastname', 'address', 'zip', 'location']))
|
return (new SingleMemberQuery($member))->getPages($type);
|
||||||
->with([
|
|
||||||
'payments' => fn ($query) => $type::paymentsQuery($query)
|
|
||||||
->orderByRaw('nr, member_id'),
|
|
||||||
])
|
|
||||||
->get()
|
|
||||||
->filter(fn (Member $member) => $member->payments->count() > 0);
|
|
||||||
|
|
||||||
return $this->toPages($members);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,15 +106,7 @@ class DocumentFactory
|
||||||
*/
|
*/
|
||||||
private function allMemberPages(string $type, BillKind $billKind): Collection
|
private function allMemberPages(string $type, BillKind $billKind): Collection
|
||||||
{
|
{
|
||||||
$members = Member::where('bill_kind', $billKind)
|
return (new BillKindQuery($billKind))->getPages($type);
|
||||||
->with([
|
|
||||||
'payments' => fn ($query) => $type::paymentsQuery($query)
|
|
||||||
->orderByRaw('nr, member_id'),
|
|
||||||
])
|
|
||||||
->get()
|
|
||||||
->filter(fn (Member $member) => $member->payments->count() > 0);
|
|
||||||
|
|
||||||
return $this->toPages($members);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Letter\Queries;
|
||||||
|
|
||||||
|
use App\Letter\BillKind;
|
||||||
|
use App\Member\Member;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
class BillKindQuery extends LetterMemberQuery
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private BillKind $billKind
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Builder<Member>
|
||||||
|
*/
|
||||||
|
protected function getQuery(): Builder
|
||||||
|
{
|
||||||
|
return Member::where('bill_kind', $this->billKind);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Letter\Queries;
|
||||||
|
|
||||||
|
use App\Letter\Letter;
|
||||||
|
use App\Letter\Page;
|
||||||
|
use App\Member\Member;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
abstract class LetterMemberQuery
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return Builder<Member>
|
||||||
|
*/
|
||||||
|
abstract protected function getQuery(): Builder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param class-string<Letter> $type
|
||||||
|
*
|
||||||
|
* @return Collection<int, Page>
|
||||||
|
*/
|
||||||
|
public function getPages(string $type): Collection
|
||||||
|
{
|
||||||
|
return $this->get($type)->groupBy(
|
||||||
|
fn ($member) => Str::slug(
|
||||||
|
"{$member->lastname}{$member->address}{$member->zip}{$member->location}",
|
||||||
|
),
|
||||||
|
)->map(fn ($page) => new Page($page));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param class-string<Letter> $type
|
||||||
|
*
|
||||||
|
* @return EloquentCollection<Member>
|
||||||
|
*/
|
||||||
|
private function get(string $type): EloquentCollection
|
||||||
|
{
|
||||||
|
return $this->getQuery()
|
||||||
|
->with([
|
||||||
|
'payments' => fn ($query) => $type::paymentsQuery($query)
|
||||||
|
->orderByRaw('nr, member_id'),
|
||||||
|
])
|
||||||
|
->get()
|
||||||
|
->filter(fn (Member $member) => $member->payments->count() > 0);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Letter\Queries;
|
||||||
|
|
||||||
|
use App\Member\Member;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
class SingleMemberQuery extends LetterMemberQuery
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private Member $member
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Builder<Member>
|
||||||
|
*/
|
||||||
|
protected function getQuery(): Builder
|
||||||
|
{
|
||||||
|
return Member::where($this->member->only(['lastname', 'address', 'zip', 'location']));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue