Completed
Pull Request — master (#2)
by Vojtěch
10:05
created

TCancelPasswordRequestPresenter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
triggerSuccess() 0 1 ?
triggerError() 0 1 ?
A injectPasswordRequestManager() 0 4 1
A startup() 0 9 2
A actionDefault() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\UI;
6
7
use Nette;
8
use SixtyEightPublishers;
9
10
/**
11
 * Use in Presenter!
12
 *
13
 * @property-read string $action
14
 * @method void changeAction($action)
15
 */
16
trait TCancelPasswordRequestPresenter
17
{
18
	/** @var NULL|\SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager */
19
	private $passwordRequestManager;
20
21
	/**
22
	 * @param \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest
23
	 *
24
	 * @return void
25
	 */
26
	abstract protected function triggerSuccess(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest): void;
27
28
	/**
29
	 * @param \SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException $e
30
	 *
31
	 * @return void
32
	 */
33
	abstract protected function triggerError(SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException $e): void;
34
35
	/**
36
	 * @internal
37
	 *
38
	 * @param \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager $passwordRequestManager
39
	 *
40
	 * @return void
41
	 */
42
	public function injectPasswordRequestManager(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager $passwordRequestManager): void
43
	{
44
		$this->passwordRequestManager = $passwordRequestManager;
45
	}
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50
	public function startup(): void
51
	{
52
		/** @noinspection PhpUndefinedClassInspection */
53
		parent::startup();
54
55
		if ($this->action !== 'default') {
56
			$this->changeAction('default');
57
		}
58
	}
59
60
	/**
61
	 * @param string $uid User UUID
62
	 * @param string $rid PasswordRequest UUID
63
	 *
64
	 * @return void
65
	 * @throws \Nette\Application\BadRequestException
66
	 */
67
	public function actionDefault(string $uid, string $rid): void
68
	{
69
		try {
70
			$this->passwordRequestManager->cancel(
71
				$request = $this->passwordRequestManager->findRequest($uid, $rid)
72
			);
73
74
			$this->triggerSuccess($request);
75
		} catch (SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException $e) {
76
			$this->triggerError($e);
77
78
			throw new Nette\Application\BadRequestException();
79
		}
80
	}
81
}
82