Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
Referentiel | |
0.00% |
0 / 50 |
|
0.00% |
0 / 6 |
306 | |
0.00% |
0 / 1 |
getModelClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
findAllByCollectivity | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
count | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
findPaginated | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
addTableOrder | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
addTableSearches | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Infrastructure\ORM\Maturity\Repository; |
25 | |
26 | use App\Application\Doctrine\Repository\CRUDRepository; |
27 | use App\Application\Traits\RepositoryUtils; |
28 | use App\Domain\Maturity\Model; |
29 | use App\Domain\Maturity\Repository; |
30 | use App\Domain\User\Model\Collectivity; |
31 | use Doctrine\ORM\QueryBuilder; |
32 | use Doctrine\ORM\Tools\Pagination\Paginator; |
33 | |
34 | class Referentiel extends CRUDRepository implements Repository\Referentiel |
35 | { |
36 | use RepositoryUtils; |
37 | |
38 | protected function getModelClass(): string |
39 | { |
40 | return Model\Referentiel::class; |
41 | } |
42 | |
43 | public function findAllByCollectivity(Collectivity $collectivity) |
44 | { |
45 | $qb = $this->createQueryBuilder(); |
46 | |
47 | $qb->leftJoin('o.authorizedCollectivities', 'c'); |
48 | $qb->andWhere('c = :collectivity'); |
49 | $qb->setParameter('collectivity', $collectivity); |
50 | $qb = $qb->getQuery(); |
51 | $qb->setFirstResult(0); |
52 | $qb->setMaxResults(100); |
53 | |
54 | return new Paginator($qb); |
55 | } |
56 | |
57 | public function count(array $criteria = []) |
58 | { |
59 | $qb = $this->createQueryBuilder(); |
60 | |
61 | $qb->select('COUNT(o.id)'); |
62 | |
63 | return $qb->getQuery()->getSingleScalarResult(); |
64 | } |
65 | |
66 | public function findPaginated($firstResult, $maxResults, $orderColumn, $orderDir, $searches, $criteria = []) |
67 | { |
68 | $qb = $this->createQueryBuilder(); |
69 | |
70 | $this->addTableSearches($qb, $searches); |
71 | $this->addTableOrder($qb, $orderColumn, $orderDir); |
72 | |
73 | $qb = $qb->getQuery(); |
74 | $qb->setFirstResult($firstResult); |
75 | $qb->setMaxResults($maxResults); |
76 | |
77 | return new Paginator($qb); |
78 | } |
79 | |
80 | private function addTableOrder(QueryBuilder $queryBuilder, $orderColumn, $orderDir) |
81 | { |
82 | switch ($orderColumn) { |
83 | case 'name': |
84 | $queryBuilder->addOrderBy('o.name', $orderDir); |
85 | break; |
86 | case 'description': |
87 | $queryBuilder->addOrderBy('o.description', $orderDir); |
88 | break; |
89 | case 'updatedAt': |
90 | $queryBuilder->addOrderBy('o.updatedAt', $orderDir); |
91 | break; |
92 | case 'createdAt': |
93 | $queryBuilder->addOrderBy('o.createdAt', $orderDir); |
94 | break; |
95 | } |
96 | } |
97 | |
98 | private function addTableSearches(QueryBuilder $queryBuilder, $searches) |
99 | { |
100 | foreach ($searches as $columnName => $search) { |
101 | switch ($columnName) { |
102 | case 'name': |
103 | $this->addWhereClause($queryBuilder, 'name', '%' . $search . '%', 'LIKE'); |
104 | break; |
105 | case 'description': |
106 | $this->addWhereClause($queryBuilder, 'description', '%' . $search . '%', 'LIKE'); |
107 | break; |
108 | case 'updatedAt': |
109 | if (is_string($search)) { |
110 | $queryBuilder->andWhere('o.updatedAt BETWEEN :updated_start_date AND :updated_finish_date') |
111 | ->setParameter('updated_start_date', date_create_from_format('d/m/y', substr($search, 0, 8))->format('Y-m-d 00:00:00')) |
112 | ->setParameter('updated_finish_date', date_create_from_format('d/m/y', substr($search, 11, 8))->format('Y-m-d 23:59:59')); |
113 | } |
114 | break; |
115 | case 'createdAt': |
116 | if (is_string($search)) { |
117 | $queryBuilder->andWhere('o.createdAt BETWEEN :create_start_date AND :create_finish_date') |
118 | ->setParameter('create_start_date', date_create_from_format('d/m/y', substr($search, 0, 8))->format('Y-m-d 00:00:00')) |
119 | ->setParameter('create_finish_date', date_create_from_format('d/m/y', substr($search, 11, 8))->format('Y-m-d 23:59:59')); |
120 | } |
121 | break; |
122 | } |
123 | } |
124 | } |
125 | } |