Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
59 / 59 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| DepositRepository | |
100.00% |
59 / 59 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getPaginatedList | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
9 | |||
| lookup | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| search | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Repository; |
| 6 | |
| 7 | use App\Entity\Deposit; |
| 8 | use App\Helper\Paginator\PaginatorOptions; |
| 9 | use App\Helper\Paginator\PaginatorRepoTrait; |
| 10 | use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
| 11 | use Doctrine\ORM\Query; |
| 12 | use Doctrine\ORM\Tools\Pagination\Paginator; |
| 13 | use Doctrine\Persistence\ManagerRegistry; |
| 14 | |
| 15 | /** |
| 16 | * DepositRepository. |
| 17 | * |
| 18 | * @method Deposit|null find($id, $lockMode = null, $lockVersion = null) |
| 19 | * @method Deposit|null findOneBy(array<string, mixed> $criteria, ?array<string, string> $orderBy = null) |
| 20 | * @method Deposit[] findAll() |
| 21 | * @method Deposit[] findBy(array<string, mixed> $criteria, ?array<string, string> $orderBy = null, $limit = null, $offset = null) |
| 22 | * |
| 23 | * @extends ServiceEntityRepository<Deposit> |
| 24 | */ |
| 25 | class DepositRepository extends ServiceEntityRepository |
| 26 | { |
| 27 | use PaginatorRepoTrait; |
| 28 | |
| 29 | public function __construct(ManagerRegistry $registry) |
| 30 | { |
| 31 | parent::__construct($registry, Deposit::class); |
| 32 | } |
| 33 | |
| 34 | public function has(Deposit $deposit): bool |
| 35 | { |
| 36 | return (bool)$this->findOneBy( |
| 37 | [ |
| 38 | 'date' => $deposit->getDate(), |
| 39 | 'document' => $deposit->getDocument(), |
| 40 | ] |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return Paginator<Query> |
| 46 | */ |
| 47 | public function getPaginatedList(PaginatorOptions $options): Paginator |
| 48 | { |
| 49 | $query = $this->createQueryBuilder('d') |
| 50 | ->orderBy('d.'.$options->getOrder(), $options->getOrderDir()); |
| 51 | |
| 52 | if ($options->searchCriteria('amount') !== '' && $options->searchCriteria('amount') !== '0') { |
| 53 | $query->andWhere('d.amount = :amount') |
| 54 | ->setParameter( |
| 55 | 'amount', |
| 56 | (float)$options->searchCriteria('amount') |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | if ($options->searchCriteria('document') !== '' && $options->searchCriteria('document') !== '0') { |
| 61 | $query->andWhere('d.document LIKE :document') |
| 62 | ->setParameter( |
| 63 | 'document', |
| 64 | '%'.(int)$options->searchCriteria('document') |
| 65 | .'%' |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | if ($options->searchCriteria('date_from') !== '' && $options->searchCriteria('date_from') !== '0') { |
| 70 | $query->andWhere('d.date >= :date_from') |
| 71 | ->setParameter( |
| 72 | 'date_from', |
| 73 | $options->searchCriteria('date_from') |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | if ($options->searchCriteria('date_to') !== '' && $options->searchCriteria('date_to') !== '0') { |
| 78 | $query->andWhere('d.date <= :date_to') |
| 79 | ->setParameter('date_to', $options->searchCriteria('date_to')); |
| 80 | } |
| 81 | |
| 82 | $query->addSelect( |
| 83 | '(SELECT t.id FROM App\Entity\Transaction t WHERE t.depId = d.id) AS tr_id' |
| 84 | ); |
| 85 | |
| 86 | $query = $query->getQuery(); |
| 87 | |
| 88 | return $this->paginate( |
| 89 | $query, |
| 90 | $options->getPage(), |
| 91 | $options->getLimit() |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return Deposit[] |
| 97 | */ |
| 98 | public function lookup(int $documentId): array |
| 99 | { |
| 100 | /** @var Deposit[] $result */ |
| 101 | $result = $this->createQueryBuilder('d') |
| 102 | ->where('d.document LIKE :document') |
| 103 | ->setParameter('document', '%'.$documentId.'%') |
| 104 | ->addSelect( |
| 105 | '(SELECT t.id FROM App\Entity\Transaction t WHERE t.depId = d.id) AS tr_id' |
| 106 | ) |
| 107 | ->setMaxResults(5) |
| 108 | ->getQuery() |
| 109 | ->getResult(); |
| 110 | |
| 111 | return $result; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return array<float> |
| 116 | */ |
| 117 | public function search(int $documentId): array |
| 118 | { |
| 119 | /** @var array<float> $result */ |
| 120 | $result = $this->createQueryBuilder('d') |
| 121 | ->leftJoin('d.transaction', 'tr') |
| 122 | ->andWhere('d.document LIKE :document') |
| 123 | ->andWhere('tr.id IS NULL') |
| 124 | ->setParameter('document', '%'.$documentId.'%') |
| 125 | ->setMaxResults(10) |
| 126 | ->getQuery() |
| 127 | ->getResult(); |
| 128 | |
| 129 | return $result; |
| 130 | } |
| 131 | } |