Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LinkCollectivitySubscriber | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
prePersist | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 |
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\Symfony\EventSubscriber\Doctrine; |
25 | |
26 | use App\Application\Symfony\Security\UserProvider; |
27 | use App\Application\Traits\Model\CollectivityTrait; |
28 | use App\Domain\User\Model\User; |
29 | use Doctrine\Common\EventSubscriber; |
30 | use Doctrine\ORM\Event\LifecycleEventArgs; |
31 | |
32 | /** |
33 | * Class LinkCollectivitySubscriber |
34 | * Link authenticated user collectivity to object. |
35 | */ |
36 | class LinkCollectivitySubscriber implements EventSubscriber |
37 | { |
38 | /** |
39 | * @var UserProvider |
40 | */ |
41 | private $userProvider; |
42 | |
43 | public function __construct(UserProvider $userProvider) |
44 | { |
45 | $this->userProvider = $userProvider; |
46 | } |
47 | |
48 | public function getSubscribedEvents(): array |
49 | { |
50 | return [ |
51 | 'prePersist', |
52 | ]; |
53 | } |
54 | |
55 | /** |
56 | * PrePersist. |
57 | * |
58 | * Link user collectivity to persisted object. |
59 | * That is to say that every time you will persist an object, |
60 | * the user collectivity will be added to related object. |
61 | * |
62 | * @throws \Exception |
63 | */ |
64 | public function prePersist(LifecycleEventArgs $args): void |
65 | { |
66 | $object = $args->getObject(); |
67 | $user = $this->userProvider->getAuthenticatedUser(); |
68 | $uses = \class_uses($object); |
69 | |
70 | if ( |
71 | \in_array(CollectivityTrait::class, $uses) |
72 | && $user instanceof User |
73 | && null === $object->getCollectivity() |
74 | ) { |
75 | $object->setCollectivity($user->getCollectivity()); |
76 | } |
77 | } |
78 | } |