1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\Common\EventSubscriber; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Events; |
8
|
|
|
use Doctrine\Common\EventSubscriber; |
9
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
10
|
|
|
use SixtyEightPublishers\User\Common\UserMapping; |
11
|
|
|
use SixtyEightPublishers\User\Common\Entity\UserInterface; |
12
|
|
|
use SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface; |
13
|
|
|
|
14
|
|
|
final class HashUserPasswordSubscriber implements EventSubscriber |
15
|
|
|
{ |
16
|
|
|
/** @var \SixtyEightPublishers\User\Common\UserMapping */ |
17
|
|
|
private $userMapping; |
18
|
|
|
|
19
|
|
|
/** @var \SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface */ |
20
|
|
|
private $passwordHashStrategy; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \SixtyEightPublishers\User\Common\UserMapping $userMapping |
24
|
|
|
* @param \SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface $passwordHashStrategy |
25
|
|
|
*/ |
26
|
|
|
public function __construct(UserMapping $userMapping, PasswordHashStrategyInterface $passwordHashStrategy) |
27
|
|
|
{ |
28
|
|
|
$this->userMapping = $userMapping; |
29
|
|
|
$this->passwordHashStrategy = $passwordHashStrategy; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public function getSubscribedEvents(): array |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
Events::onFlush, |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param \Doctrine\ORM\Event\OnFlushEventArgs $args |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function onFlush(OnFlushEventArgs $args): void |
48
|
|
|
{ |
49
|
|
|
$em = $args->getEntityManager(); |
50
|
|
|
$uow = $em->getUnitOfWork(); |
51
|
|
|
$classMetadata = $em->getClassMetadata(UserInterface::class); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $objects |
55
|
|
|
* |
56
|
|
|
* @return \SixtyEightPublishers\User\Common\Entity\UserInterface[] |
57
|
|
|
*/ |
58
|
|
|
$filter = static function (array $objects) { |
59
|
|
|
return array_filter($objects, static function ($object) { |
60
|
|
|
return $object instanceof UserInterface; |
61
|
|
|
}); |
62
|
|
|
}; |
63
|
|
|
|
64
|
|
View Code Duplication |
foreach ($filter($uow->getScheduledEntityInsertions()) as $user) { |
|
|
|
|
65
|
|
|
$password = $classMetadata->getFieldValue($user, $this->userMapping[UserMapping::FIELD_PASSWORD]); |
66
|
|
|
|
67
|
|
|
if (!$this->passwordHashStrategy->needRehash($password)) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$classMetadata->setFieldValue($user, $this->userMapping[UserMapping::FIELD_PASSWORD], $this->passwordHashStrategy->hash($password)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
foreach ($filter($uow->getScheduledEntityUpdates()) as $user) { |
|
|
|
|
75
|
|
|
$password = $classMetadata->getFieldValue($user, $this->userMapping[UserMapping::FIELD_PASSWORD]); |
76
|
|
|
|
77
|
|
|
if (!$this->passwordHashStrategy->needRehash($password)) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$classMetadata->setFieldValue($user, $this->userMapping[UserMapping::FIELD_PASSWORD], $this->passwordHashStrategy->hash($password)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.