1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\PasswordRequest; |
6
|
|
|
|
7
|
|
|
use Nette\SmartObject; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use SixtyEightPublishers\User\Common\DbalType\Password\Password; |
10
|
|
|
use SixtyEightPublishers\DoctrinePersistence\TransactionFactoryInterface; |
11
|
|
|
use SixtyEightPublishers\DoctrinePersistence\Context\ErrorContextInterface; |
12
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface; |
13
|
|
|
use SixtyEightPublishers\DoctrineQueryObjects\ExecutableQueryObjectFactoryInterface; |
14
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException; |
15
|
|
|
use SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface; |
16
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\FindPasswordRequestByIdsQueryObjectFactoryInterface; |
17
|
|
|
|
18
|
|
|
class PasswordRequestManager implements PasswordRequestManagerInterface |
19
|
|
|
{ |
20
|
|
|
use SmartObject; |
21
|
|
|
|
22
|
|
|
/** @var \SixtyEightPublishers\DoctrinePersistence\TransactionFactoryInterface */ |
23
|
|
|
private $transactionFactory; |
24
|
|
|
|
25
|
|
|
/** @var \SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface */ |
26
|
|
|
private $passwordHashStrategy; |
27
|
|
|
|
28
|
|
|
/** @var \SixtyEightPublishers\DoctrineQueryObjects\ExecutableQueryObjectFactoryInterface */ |
29
|
|
|
private $executableQueryObjectFactory; |
30
|
|
|
|
31
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\Query\FindPasswordRequestByIdsQueryObjectFactoryInterface */ |
32
|
|
|
private $findPasswordRequestByIdsQueryFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \SixtyEightPublishers\DoctrinePersistence\TransactionFactoryInterface $transactionFactory |
36
|
|
|
* @param \SixtyEightPublishers\User\Common\PasswordHashStrategy\PasswordHashStrategyInterface $passwordHashStrategy |
37
|
|
|
* @param \SixtyEightPublishers\DoctrineQueryObjects\ExecutableQueryObjectFactoryInterface $executableQueryObjectFactory |
38
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\Query\FindPasswordRequestByIdsQueryObjectFactoryInterface $findPasswordRequestByIdsQueryFactory |
39
|
|
|
*/ |
40
|
|
|
public function __construct(TransactionFactoryInterface $transactionFactory, PasswordHashStrategyInterface $passwordHashStrategy, ExecutableQueryObjectFactoryInterface $executableQueryObjectFactory, FindPasswordRequestByIdsQueryObjectFactoryInterface $findPasswordRequestByIdsQueryFactory) |
41
|
|
|
{ |
42
|
|
|
$this->transactionFactory = $transactionFactory; |
43
|
|
|
$this->passwordHashStrategy = $passwordHashStrategy; |
44
|
|
|
$this->executableQueryObjectFactory = $executableQueryObjectFactory; |
45
|
|
|
$this->findPasswordRequestByIdsQueryFactory = $findPasswordRequestByIdsQueryFactory; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function findRequest($uid, $rid): PasswordRequestInterface |
52
|
|
|
{ |
53
|
|
|
$request = $this->executableQueryObjectFactory->create($this->findPasswordRequestByIdsQueryFactory->create($uid, $rid))->fetchOne(); |
54
|
|
|
|
55
|
|
|
if (!$request instanceof PasswordRequestInterface) { |
56
|
|
|
throw PasswordRequestProcessException::missingRequest($uid, $rid); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (TRUE === $request->isExpired()) { |
60
|
|
|
throw PasswordRequestProcessException::expiredRequest($request->getUser()->getId(), $request->getId()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $request; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
* |
69
|
|
|
* @throws \Throwable |
70
|
|
|
*/ |
71
|
|
|
public function reset(PasswordRequestInterface $passwordRequest, string $password): void |
72
|
|
|
{ |
73
|
|
|
$transaction = $this->transactionFactory->create(function (EntityManagerInterface $em, PasswordRequestInterface $passwordRequest, string $password) { |
74
|
|
|
$user = $passwordRequest->getUser(); |
75
|
|
|
|
76
|
|
|
if ($this->passwordHashStrategy->needRehash($password)) { |
77
|
|
|
$password = $this->passwordHashStrategy->hash($password); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$passwordRequest->getResetDeviceInfo()->fill(); |
81
|
|
|
$passwordRequest->setStatus($passwordRequest::STATUS_COMPLETED); |
82
|
|
|
$user->setPassword(new Password($password)); |
83
|
|
|
|
84
|
|
|
$em->persist($passwordRequest); |
85
|
|
|
$em->persist($user); |
86
|
|
|
|
87
|
|
|
return $user; |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
View Code Duplication |
$transaction->error(static function (ErrorContextInterface $context) use ($passwordRequest) { |
|
|
|
|
91
|
|
|
$e = $context->getError(); |
92
|
|
|
|
93
|
|
|
if (!$e instanceof PasswordRequestProcessException) { |
94
|
|
|
throw new PasswordRequestProcessException($passwordRequest->getUser()->getId(), $passwordRequest->getId(), $e->getMessage(), 0, $e); |
95
|
|
|
} |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
$transaction->withArguments([ |
99
|
|
|
'passwordRequest' => $passwordRequest, |
100
|
|
|
'password' => $password, |
101
|
|
|
])->run(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
* |
107
|
|
|
* @throws \Throwable |
108
|
|
|
*/ |
109
|
|
|
public function cancel(PasswordRequestInterface $passwordRequest): void |
110
|
|
|
{ |
111
|
|
|
$transaction = $this->transactionFactory->create(static function (EntityManagerInterface $em, PasswordRequestInterface $passwordRequest) { |
112
|
|
|
$passwordRequest->getResetDeviceInfo()->fill(); |
113
|
|
|
$passwordRequest->setStatus($passwordRequest::STATUS_CANCELED); |
114
|
|
|
|
115
|
|
|
$em->persist($passwordRequest); |
116
|
|
|
|
117
|
|
|
return $passwordRequest; |
118
|
|
|
}); |
119
|
|
|
|
120
|
|
View Code Duplication |
$transaction->error(static function (ErrorContextInterface $context) use ($passwordRequest) { |
|
|
|
|
121
|
|
|
$e = $context->getError(); |
122
|
|
|
|
123
|
|
|
if (!$e instanceof PasswordRequestProcessException) { |
124
|
|
|
throw new PasswordRequestProcessException($passwordRequest->getUser()->getId(), $passwordRequest->getId(), $e->getMessage(), 0, $e); |
125
|
|
|
} |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
$transaction->withArguments(['passwordRequest' => $passwordRequest])->run(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.