PasswordRequestFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 38 3
A createPasswordRequestEntity() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\PasswordRequest;
6
7
use Nette\SmartObject;
8
use Doctrine\ORM\AbstractQuery;
9
use Doctrine\ORM\EntityManagerInterface;
10
use SixtyEightPublishers\User\ForgotPassword\Entity\UserInterface;
11
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequest;
12
use SixtyEightPublishers\DoctrinePersistence\TransactionFactoryInterface;
13
use SixtyEightPublishers\DoctrineQueryObjects\ResultSet\ResultSetOptions;
14
use SixtyEightPublishers\DoctrinePersistence\Context\ErrorContextInterface;
15
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface;
16
use SixtyEightPublishers\DoctrineQueryObjects\ExecutableQueryObjectFactoryInterface;
17
use SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestCreationException;
18
use SixtyEightPublishers\User\ForgotPassword\Query\GetUserByEmailQueryObjectFactoryInterface;
19
use SixtyEightPublishers\User\ForgotPassword\Query\CancelPasswordRequestsByUserQueryObjectFactoryInterface;
20
21
class PasswordRequestFactory implements PasswordRequestFactoryInterface
22
{
23
	use SmartObject;
24
25
	/** @var \SixtyEightPublishers\DoctrinePersistence\TransactionFactoryInterface  */
26
	private $transactionFactory;
27
28
	/** @var \SixtyEightPublishers\DoctrineQueryObjects\ExecutableQueryObjectFactoryInterface  */
29
	private $executableQueryObjectFactory;
30
31
	/** @var \SixtyEightPublishers\User\ForgotPassword\Query\GetUserByEmailQueryObjectFactoryInterface  */
32
	private $getUserByEmailQueryFactory;
33
34
	/** @var \SixtyEightPublishers\User\ForgotPassword\Query\CancelPasswordRequestsByUserQueryObjectFactoryInterface  */
35
	private $cancelPasswordRequestsByUserQueryFactory;
36
37
	/**
38
	 * @param \SixtyEightPublishers\DoctrinePersistence\TransactionFactoryInterface                                   $transactionFactory
39
	 * @param \SixtyEightPublishers\DoctrineQueryObjects\ExecutableQueryObjectFactoryInterface                        $executableQueryObjectFactory
40
	 * @param \SixtyEightPublishers\User\ForgotPassword\Query\GetUserByEmailQueryObjectFactoryInterface               $getUserByEmailQueryFactory
41
	 * @param \SixtyEightPublishers\User\ForgotPassword\Query\CancelPasswordRequestsByUserQueryObjectFactoryInterface $cancelPasswordRequestsByUserQueryFactory
42
	 */
43
	public function __construct(TransactionFactoryInterface $transactionFactory, ExecutableQueryObjectFactoryInterface $executableQueryObjectFactory, GetUserByEmailQueryObjectFactoryInterface $getUserByEmailQueryFactory, CancelPasswordRequestsByUserQueryObjectFactoryInterface $cancelPasswordRequestsByUserQueryFactory)
44
	{
45
		$this->transactionFactory = $transactionFactory;
46
		$this->executableQueryObjectFactory = $executableQueryObjectFactory;
47
		$this->getUserByEmailQueryFactory = $getUserByEmailQueryFactory;
48
		$this->cancelPasswordRequestsByUserQueryFactory = $cancelPasswordRequestsByUserQueryFactory;
49
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 *
54
	 * @throws \Throwable
55
	 */
56
	public function create(string $email): PasswordRequestInterface
57
	{
58
		$transaction = $this->transactionFactory->create(function (string $email) {
59
			$user = $this->executableQueryObjectFactory
60
				->create($this->getUserByEmailQueryFactory->create($email))
61
				->fetchOne();
62
63
			if (NULL === $user) {
64
				throw PasswordRequestCreationException::notRegisteredEmail($email);
65
			}
66
67
			return $user;
68
		});
69
70
		$transaction->then(function (EntityManagerInterface $em, UserInterface $result) {
71
			$request = $this->createPasswordRequestEntity($result);
72
73
			$this->executableQueryObjectFactory->create(
74
				$this->cancelPasswordRequestsByUserQueryFactory->create($result),
75
				(new ResultSetOptions())->setHydrationMode(AbstractQuery::HYDRATE_SCALAR)
76
			)->fetch();
77
78
			$request->getRequestDeviceInfo()->fill();
79
			$em->persist($request);
80
81
			return $request;
82
		});
83
84
		$transaction->error(static function (ErrorContextInterface $context) {
85
			$e = $context->getError();
86
87
			if (!$e instanceof PasswordRequestCreationException) {
88
				throw new PasswordRequestCreationException($e->getMessage(), $e->getCode(), $e);
89
			}
90
		});
91
92
		return $transaction->withArguments(['email' => $email])->run();
93
	}
94
95
	/**
96
	 * @param \SixtyEightPublishers\User\ForgotPassword\Entity\UserInterface $user
97
	 *
98
	 * @return \SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface
99
	 * @throws \Exception
100
	 */
101
	protected function createPasswordRequestEntity(UserInterface $user): PasswordRequestInterface
102
	{
103
		return new PasswordRequest($user);
104
	}
105
}
106