Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.65% |
22 / 23 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
LoginAttempt | |
95.65% |
22 / 23 |
|
85.71% |
6 / 7 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModelClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
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% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
findOneOrNullByIpAndEmail | |
100.00% |
8 / 8 |
|
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\Infrastructure\ORM\User\Repository; |
25 | |
26 | use App\Domain\User\Model; |
27 | use App\Domain\User\Repository; |
28 | use Doctrine\ORM\EntityManagerInterface; |
29 | use Doctrine\ORM\QueryBuilder; |
30 | use Doctrine\Persistence\ManagerRegistry; |
31 | |
32 | class LoginAttempt implements Repository\LoginAttempt |
33 | { |
34 | protected ManagerRegistry $registry; |
35 | |
36 | protected EntityManagerInterface $manager; |
37 | |
38 | public function __construct(ManagerRegistry $registry) |
39 | { |
40 | $this->registry = $registry; |
41 | } |
42 | |
43 | protected function getModelClass(): string |
44 | { |
45 | return Model\LoginAttempt::class; |
46 | } |
47 | |
48 | /** |
49 | * @throws \Exception |
50 | */ |
51 | protected function getManager(): EntityManagerInterface |
52 | { |
53 | $manager = $this->registry->getManager(); |
54 | |
55 | if (!$manager instanceof EntityManagerInterface) { |
56 | throw new \Exception('Registry Manager must be an instance of EntityManagerInterface'); |
57 | } |
58 | |
59 | return $manager; |
60 | } |
61 | |
62 | /** |
63 | * @throws \Exception |
64 | * |
65 | * @return QueryBuilder |
66 | */ |
67 | protected function createQueryBuilder() |
68 | { |
69 | return $this->getManager() |
70 | ->createQueryBuilder() |
71 | ->select('o') |
72 | ->from($this->getModelClass(), 'o') |
73 | ; |
74 | } |
75 | |
76 | /** |
77 | * @param Model\User $object |
78 | * |
79 | * @throws \Exception |
80 | */ |
81 | public function insert($object): void |
82 | { |
83 | $this->getManager()->persist($object); |
84 | $this->getManager()->flush(); |
85 | } |
86 | |
87 | /** |
88 | * @param Model\User $object |
89 | * |
90 | * @throws \Exception |
91 | */ |
92 | public function update($object): void |
93 | { |
94 | $v = $object; |
95 | $this->getManager()->flush(); |
96 | } |
97 | |
98 | /** |
99 | * @throws \Doctrine\ORM\NonUniqueResultException |
100 | */ |
101 | public function findOneOrNullByIpAndEmail(string $ip, string $email): ?Model\LoginAttempt |
102 | { |
103 | return $this->createQueryBuilder() |
104 | ->andWhere('o.ip = :ip') |
105 | ->andWhere('o.email = :email') |
106 | ->setParameter('ip', $ip) |
107 | ->setParameter('email', $email) |
108 | ->getQuery() |
109 | ->getOneOrNullResult() |
110 | ; |
111 | } |
112 | } |