1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DoyoUserBundle project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\UserBundle\Bridge\ORM; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\EventSubscriber; |
17
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
18
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
19
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
|
|
|
20
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
21
|
|
|
use Doctrine\ORM\Events; |
22
|
|
|
use Doyo\UserBundle\Model\UserInterface; |
23
|
|
|
use Doyo\UserBundle\Util\CanonicalFieldsUpdaterInterface; |
24
|
|
|
use Doyo\UserBundle\Util\PasswordUpdaterInterface; |
25
|
|
|
|
26
|
|
|
class UserListener implements EventSubscriber |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var PasswordUpdaterInterface |
30
|
|
|
*/ |
31
|
|
|
private $passwordUpdater; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CanonicalFieldsUpdaterInterface |
35
|
|
|
*/ |
36
|
|
|
private $canonicalFieldsUpdater; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* UserListener constructor. |
40
|
|
|
*/ |
41
|
21 |
|
public function __construct(PasswordUpdaterInterface $passwordUpdater, CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater) |
42
|
|
|
{ |
43
|
21 |
|
$this->passwordUpdater = $passwordUpdater; |
44
|
21 |
|
$this->canonicalFieldsUpdater = $canonicalFieldsUpdater; |
45
|
|
|
} |
46
|
|
|
|
47
|
16 |
|
public function getSubscribedEvents() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
16 |
|
Events::prePersist, |
51
|
16 |
|
Events::preUpdate, |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Pre persist listener based on doctrine common. |
57
|
|
|
*/ |
58
|
11 |
|
public function prePersist(LifecycleEventArgs $args) |
59
|
|
|
{ |
60
|
11 |
|
$object = $args->getObject(); |
61
|
11 |
|
if ($object instanceof UserInterface) { |
62
|
7 |
|
$this->updateUserFields($object); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Pre update listener based on doctrine common. |
68
|
|
|
*/ |
69
|
13 |
|
public function preUpdate(LifecycleEventArgs $args) |
70
|
|
|
{ |
71
|
13 |
|
$object = $args->getObject(); |
72
|
13 |
|
if ($object instanceof UserInterface) { |
73
|
13 |
|
$this->updateUserFields($object); |
74
|
13 |
|
$this->recomputeChangeSet($args->getObjectManager(), $object); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Updates the user properties. |
80
|
|
|
*/ |
81
|
14 |
|
private function updateUserFields(UserInterface $user) |
82
|
|
|
{ |
83
|
14 |
|
$this->canonicalFieldsUpdater->updateCanonicalFields($user); |
84
|
14 |
|
$this->passwordUpdater->hashPassword($user); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Recomputes change set for Doctrine implementations not doing it automatically after the event. |
89
|
|
|
*/ |
90
|
13 |
|
private function recomputeChangeSet(ObjectManager $om, UserInterface $user) |
91
|
|
|
{ |
92
|
13 |
|
$meta = $om->getClassMetadata(\get_class($user)); |
93
|
|
|
|
94
|
13 |
|
if ($om instanceof EntityManagerInterface) { |
95
|
11 |
|
$om->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $user); |
96
|
|
|
|
97
|
11 |
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if ($om instanceof DocumentManager) { |
101
|
|
|
$om->getUnitOfWork()->recomputeSingleDocumentChangeSet($meta, $user); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths