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

PasswordRequest::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\ForgotPassword\DoctrineEntity;
6
7
use Nette;
8
use Ramsey;
9
use SixtyEightPublishers;
10
use Doctrine\ORM\Mapping as ORM;
11
12
/**
13
 * @ORM\Entity
14
 * @ORM\HasLifecycleCallbacks
15
 */
16
class PasswordRequest implements IPasswordRequest
17
{
18
	use Nette\SmartObject;
19
20
	/**
21
	 * @ORM\Id
22
	 * @ORM\Column(type="uuid", unique=true)
23
	 *
24
	 * @var \Ramsey\Uuid\UuidInterface
25
	 */
26
	private $id;
27
28
	/**
29
	 * @ORM\Column(type="string", length=15)
30
	 *
31
	 * @var string
32
	 */
33
	private $status;
34
35
	/**
36
	 * @ORM\Column(type="datetime")
37
	 *
38
	 * @var \DateTime
39
	 */
40
	private $created;
41
42
	/**
43
	 * @ORM\Column(type="datetime")
44
	 *
45
	 * @var \DateTime
46
	 */
47
	private $updated;
48
49
	/**
50
	 * @ORM\ManyToOne(targetEntity="IUser")
51
	 * @ORM\JoinColumn(onDelete="CASCADE")
52
	 *
53
	 * @var \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IUser
54
	 */
55
	private $user;
56
57
	/**
58
	 * @ORM\Embedded(class="DeviceInfo", columnPrefix="request_")
59
	 *
60
	 * @var \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\DeviceInfo
61
	 */
62
	private $requestDeviceInfo;
63
64
	/**
65
	 * @ORM\Embedded(class="DeviceInfo", columnPrefix="reset_")
66
	 *
67
	 * @var \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\DeviceInfo
68
	 */
69
	private $resetDeviceInfo;
70
71
	/** @var string  */
72
	private static $expiration = self::DEFAULT_EXPIRATION;
73
74
	/**
75
	 * @param \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IUser $user
76
	 */
77
	public function __construct(IUser $user)
78
	{
79
		$this->id = Ramsey\Uuid\Uuid::uuid4();
80
		$this->user = $user;
81
		$this->status = self::STATUS_CREATED;
82
		$this->created = new \DateTime('now', new \DateTimeZone('UTC'));
83
		$this->updated = new \DateTime('now', new \DateTimeZone('UTC'));
84
		$this->requestDeviceInfo = new DeviceInfo();
85
		$this->resetDeviceInfo = new DeviceInfo();
86
	}
87
88
	/**
89
	 * @internal
90
	 * @ORM\PreUpdate
91
	 *
92
	 * @return void
93
	 */
94
	public function onPreUpdate(): void
95
	{
96
		$this->updated = new \DateTime('now');
97
	}
98
99
	/**
100
	 * @param string $expiration
101
	 *
102
	 * @return void
103
	 */
104
	public static function setExpirationString(string $expiration): void
105
	{
106
		self::$expiration = $expiration;
107
	}
108
109
	/**************** interface \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest ****************/
110
111
	/**
112
	 * @return \Ramsey\Uuid\UuidInterface
113
	 */
114
	public function getId(): Ramsey\Uuid\UuidInterface
115
	{
116
		return $this->id;
117
	}
118
119
	/**
120
	 * {@inheritdoc}
121
	 */
122
	public function getStatus(): string
123
	{
124
		return $this->status;
125
	}
126
127
	/**
128
	 * {@inheritdoc}
129
	 */
130
	public function setStatus(string $status): void
131
	{
132
		if (!in_array($status, self::STATUSES)) {
133
			throw new SixtyEightPublishers\User\Common\Exception\InvalidArgumentException(sprintf(
134
				'Value %s is not in allowed set [%s]',
135
				(string) $status,
136
				implode(', ', array_map('strval', self::STATUSES))
137
			));
138
		}
139
140
		$this->status = $status;
141
	}
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146
	public function getCreated(): \DateTime
147
	{
148
		return $this->created;
149
	}
150
151
	/**
152
	 * {@inheritdoc}
153
	 */
154
	public function getUpdated(): \DateTime
155
	{
156
		return $this->updated;
157
	}
158
159
	/**
160
	 * {@inheritdoc}
161
	 */
162
	public function getUser(): IUser
163
	{
164
		return $this->user;
165
	}
166
167
	/**
168
	 * {@inheritdoc}
169
	 */
170
	public function getExpiration(): \DateTime
171
	{
172
		return (clone $this->created)->modify(self::$expiration);
173
	}
174
175
	/**
176
	 * {@inheritdoc}
177
	 */
178
	public function isExpired(): bool
179
	{
180
		return $this->getExpiration() < new \DateTime('now', new \DateTimeZone('UTC'));
181
	}
182
183
	/**
184
	 * {@inheritdoc}
185
	 */
186
	public function getRequestDeviceInfo(): DeviceInfo
187
	{
188
		return $this->requestDeviceInfo;
189
	}
190
191
	/**
192
	 * {@inheritdoc}
193
	 */
194
	public function getResetDeviceInfo(): DeviceInfo
195
	{
196
		return $this->resetDeviceInfo;
197
	}
198
}
199