Co-authored-by: manoel freitas <manoel.josefneto@gmail.com> Co-committed-by: manoel freitas <manoel.josefneto@gmail.com>
52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Application\Queries\Marketing;
|
|
|
|
use App\Domain\Marketing\PortfolioVertical;
|
|
use App\Models\PortfolioCase;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
final class GetPublishedPortfolioCases
|
|
{
|
|
/**
|
|
* @return Collection<int, PortfolioCase>
|
|
*/
|
|
public function __invoke(?PortfolioVertical $vertical = null): Collection
|
|
{
|
|
return $this->baseQuery($vertical)->get();
|
|
}
|
|
|
|
/**
|
|
* @return LengthAwarePaginator<int, PortfolioCase>
|
|
*/
|
|
public function paginate(?PortfolioVertical $vertical = null, int $perPage = 9): LengthAwarePaginator
|
|
{
|
|
return $this->baseQuery($vertical)->paginate($perPage)->withQueryString();
|
|
}
|
|
|
|
/**
|
|
* @return Builder<PortfolioCase>
|
|
*/
|
|
private function baseQuery(?PortfolioVertical $vertical): Builder
|
|
{
|
|
$query = PortfolioCase::query()
|
|
->published()
|
|
->with(['images'])
|
|
->orderBy('sort_order');
|
|
|
|
if ($vertical !== null) {
|
|
$query->where(function (Builder $builder) use ($vertical): void {
|
|
foreach ($vertical->eventTypePatterns() as $pattern) {
|
|
$builder->orWhere('event_type', 'ilike', $pattern);
|
|
}
|
|
});
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
}
|