Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.14% |
34 / 35 |
|
90.00% |
9 / 10 |
CRAP | |
0.00% |
0 / 1 |
CRUDRepository | |
97.14% |
34 / 35 |
|
90.00% |
9 / 10 |
12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModelClass | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getManager | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
createQueryBuilder | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
insert | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
update | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
remove | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
findAll | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
findOneById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
findBy | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
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\Application\Doctrine\Repository; |
25 | |
26 | use App\Application\DDD\Repository\CRUDRepositoryInterface; |
27 | use Doctrine\ORM\EntityManagerInterface; |
28 | use Doctrine\Persistence\ManagerRegistry; |
29 | |
30 | /** |
31 | * Abstract Class CRUDRepository |
32 | * The goal of this class if to pre-define Infrastructure of repositories. |
33 | */ |
34 | abstract class CRUDRepository implements CRUDRepositoryInterface |
35 | { |
36 | /** |
37 | * @var ManagerRegistry |
38 | */ |
39 | protected $registry; |
40 | |
41 | /** |
42 | * CRUDRepository constructor. |
43 | */ |
44 | public function __construct(ManagerRegistry $registry) |
45 | { |
46 | $this->registry = $registry; |
47 | } |
48 | |
49 | /** |
50 | * Get the model class name. |
51 | */ |
52 | abstract protected function getModelClass(): string; |
53 | |
54 | /** |
55 | * Get the registry manager |
56 | * Since we use Doctrine, we expect to get EntityManagerInterface. |
57 | * |
58 | * @throws \Exception |
59 | */ |
60 | protected function getManager(): EntityManagerInterface |
61 | { |
62 | $manager = $this->registry->getManager(); |
63 | |
64 | if (!$manager instanceof EntityManagerInterface) { |
65 | throw new \Exception('Registry Manager must be an instance of EntityManagerInterface'); |
66 | } |
67 | |
68 | return $manager; |
69 | } |
70 | |
71 | /** |
72 | * Create the base of QueryBuilder to use for repository calls. |
73 | * |
74 | * @throws \Exception |
75 | */ |
76 | protected function createQueryBuilder(): \Doctrine\ORM\QueryBuilder |
77 | { |
78 | return $this->getManager() |
79 | ->createQueryBuilder() |
80 | ->select('o') |
81 | ->from($this->getModelClass(), 'o') |
82 | ; |
83 | } |
84 | |
85 | /** |
86 | * Insert an object. |
87 | * |
88 | * @throws \Exception |
89 | */ |
90 | public function insert($object): void |
91 | { |
92 | $this->getManager()->persist($object); |
93 | $this->getManager()->flush(); |
94 | } |
95 | |
96 | /** |
97 | * Update an object. |
98 | * |
99 | * @throws \Exception |
100 | */ |
101 | public function update($object): void |
102 | { |
103 | $this->getManager()->flush(); |
104 | } |
105 | |
106 | /** |
107 | * Create an object. |
108 | */ |
109 | public function create() |
110 | { |
111 | $class = $this->getModelClass(); |
112 | |
113 | return new $class(); |
114 | } |
115 | |
116 | /** |
117 | * Remove an object. |
118 | * |
119 | * @throws \Exception |
120 | */ |
121 | public function remove($object): void |
122 | { |
123 | $this->getManager()->remove($object); |
124 | $this->getManager()->flush(); |
125 | } |
126 | |
127 | /** |
128 | * Get all objects. |
129 | * |
130 | * @return mixed[] |
131 | */ |
132 | public function findAll(array $order = []): array |
133 | { |
134 | $orderBy = []; |
135 | foreach ($order as $key => $value) { |
136 | $orderBy[$key] = $value; |
137 | } |
138 | |
139 | return $this->registry |
140 | ->getManager() |
141 | ->getRepository($this->getModelClass()) |
142 | ->findBy([], $orderBy) |
143 | ; |
144 | } |
145 | |
146 | /** |
147 | * Get an object by ID. |
148 | * |
149 | * @param string $id The ID to find |
150 | * |
151 | * @return object|null |
152 | */ |
153 | public function findOneById(string $id) |
154 | { |
155 | return $this->registry |
156 | ->getManager() |
157 | ->getRepository($this->getModelClass()) |
158 | ->find($id) |
159 | ; |
160 | } |
161 | |
162 | public function findBy(array $criterias) |
163 | { |
164 | return $this->registry |
165 | ->getManager() |
166 | ->getRepository($this->getModelClass()) |
167 | ->findBy($criterias) |
168 | ; |
169 | } |
170 | } |