Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ListOwnersQuery
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Owners\Application\Queries;
6
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use ImovelZapAi\Owners\Infrastructure\Persistence\Eloquent\OwnerModel;
9
10final class ListOwnersQuery
11{
12    /**
13     * @param  array{search?: string|null, per_page?: int}  $filters
14     */
15    public function execute(string $tenantId, array $filters = []): LengthAwarePaginator
16    {
17        $query = OwnerModel::query()
18            ->where('tenant_id', $tenantId)
19            ->with(['contacts', 'responsible'])
20            ->latest();
21
22        if (! empty($filters['search'])) {
23            $search = '%'.$filters['search'].'%';
24            $query->where(function ($q) use ($search): void {
25                $q->where('name', 'ilike', $search)
26                    ->orWhere('document', 'ilike', $search)
27                    ->orWhere('trade_name', 'ilike', $search);
28            });
29        }
30
31        return $query->paginate((int) ($filters['per_page'] ?? 20));
32    }
33}