Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
EncodePasswordSubscriber
94.44% covered (success)
94.44%
17 / 18
80.00% covered (warning)
80.00%
4 / 5
9.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSubscribedEvents
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 prePersist
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 preUpdate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 encodePassword
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
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
22declare(strict_types=1);
23
24namespace App\Domain\User\Symfony\EventSubscriber\Doctrine;
25
26use App\Domain\User\Model\User;
27use Doctrine\Common\EventSubscriber;
28use Doctrine\ORM\Event\LifecycleEventArgs;
29use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
30
31class EncodePasswordSubscriber implements EventSubscriber
32{
33    /**
34     * @var UserPasswordEncoderInterface
35     */
36    private $passwordEncoder;
37
38    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
39    {
40        $this->passwordEncoder = $passwordEncoder;
41    }
42
43    public function getSubscribedEvents()
44    {
45        return [
46            'prePersist',
47            'preUpdate',
48        ];
49    }
50
51    /**
52     * PrePersist
53     * - User : If plainPassword is set, hash it and set password.
54     *
55     * @throws \Exception
56     */
57    public function prePersist(LifecycleEventArgs $args): void
58    {
59        if (!$args->getObject() instanceof User) {
60            return;
61        }
62
63        $this->encodePassword($args);
64    }
65
66    /**
67     * PreUpdate
68     * - User : If plainPassword is set, hash it and set password.
69     *
70     * @throws \Exception
71     */
72    public function preUpdate(LifecycleEventArgs $args): void
73    {
74        if (!$args->getObject() instanceof User) {
75            return;
76        }
77
78        $this->encodePassword($args);
79    }
80
81    /**
82     * Encode plainPassword in password field.
83     */
84    public function encodePassword(LifecycleEventArgs $args): void
85    {
86        $model = $args->getObject();
87
88        if (!$model instanceof User) {
89            return;
90        }
91
92        if (\is_null($model->getPlainPassword())) {
93            return;
94        }
95
96        $model->setPassword($this->passwordEncoder->encodePassword($model, $model->getPlainPassword()));
97        $model->eraseCredentials();
98    }
99}