Completed
Pull Request — master (#2)
by Tomáš
09:22
created

PasswordRequestManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A tryCatch() 0 12 3
A findRequest() 0 21 4
A reset() 0 18 2
A cancel() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\PasswordRequest;
6
7
use Nette;
8
use Doctrine;
9
use SixtyEightPublishers;
10
11
class PasswordRequestManager implements IPasswordRequestManager
12
{
13
	use Nette\SmartObject;
14
15
	/** @var \Doctrine\ORM\EntityManagerInterface  */
16
	private $em;
17
18
	/** @var \SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy  */
19
	private $passwordHashStrategy;
20
21
	/** @var \SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory  */
22
	private $findPasswordRequestByIdsQueryFactory;
23
24
	/**
25
	 * @param \Doctrine\ORM\EntityManagerInterface                                                  $em
26
	 * @param \SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy          $passwordHashStrategy
27
	 * @param \SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory $findPasswordRequestByIdsQueryFactory
28
	 */
29
	public function __construct(
30
		Doctrine\ORM\EntityManagerInterface $em,
31
		SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy $passwordHashStrategy,
32
		SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory $findPasswordRequestByIdsQueryFactory
33
	) {
34
		$this->em = $em;
35
		$this->passwordHashStrategy = $passwordHashStrategy;
36
		$this->findPasswordRequestByIdsQueryFactory = $findPasswordRequestByIdsQueryFactory;
37
	}
38
39
	/**
40
	 * @param \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request
41
	 * @param callable                                                                  $try
42
	 *
43
	 * @return mixed
44
	 * @throws \SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException
45
	 */
46
	private function tryCatch(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request, callable $try)
47
	{
48
		try {
49
			return $try($request);
50
		} catch (\Throwable $e) {
51
			if (!$e instanceof SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException) {
52
				$e = new SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException($request->getUser()->getId(), $request->getId(), $e->getMessage(), 0, $e);
53
			}
54
55
			throw $e;
56
		}
57
	}
58
59
	/*********** interface \SixtyEightPublishers\User\ForgotPassword\IPasswordRequestSender ***********/
60
61
	/**
62
	 * {@inheritdoc}
63
	 */
64
	public function findRequest($uid, $rid): SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest
65
	{
66
		try {
67
			/** @var \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request */
68
			$request = $this->findPasswordRequestByIdsQueryFactory
69
				->create($this->em, $uid, $rid)
70
				->getOneOrNullResult();
71
		} catch (Doctrine\DBAL\DBALException $e) {
72
			throw SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException::missingRequest($uid, $rid);
73
		}
74
75
		if (NULL === $request) {
76
			throw SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException::missingRequest($uid, $rid);
77
		}
78
79
		if (TRUE === $request->isExpired()) {
80
			throw SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException::expiredRequest($request->getUser()->getId(), $request->getId());
81
		}
82
83
		return $request;
84
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89
	public function reset(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest, string $password): void
90
	{
91
		$this->tryCatch($passwordRequest, function (SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest) use ($password) {
92
			$user = $passwordRequest->getUser();
93
94
			if ($this->passwordHashStrategy->needRehash($password)) {
95
				$password = $this->passwordHashStrategy->hash($password);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $password, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
96
			}
97
98
			$passwordRequest->getResetDeviceInfo()->fill();
99
			$passwordRequest->setStatus($passwordRequest::STATUS_COMPLETED);
100
			$user->setPassword($password);
101
102
			$this->em->persist($passwordRequest);
103
			$this->em->persist($user);
104
			$this->em->flush();
105
		});
106
	}
107
108
	/**
109
	 * {@inheritdoc}
110
	 */
111
	public function cancel(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest): void
112
	{
113
		$this->tryCatch($passwordRequest, function (SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest) {
114
			$passwordRequest->getResetDeviceInfo()->fill();
115
			$passwordRequest->setStatus($passwordRequest::STATUS_CANCELED);
116
117
			$this->em->persist($passwordRequest);
118
			$this->em->flush();
119
		});
120
	}
121
}
122