|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Controller; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Password; |
|
12
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry; |
|
13
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory; |
|
14
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory; |
|
15
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory; |
|
16
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
|
17
|
|
|
use Zend\Hydrator\HydratorInterface; |
|
18
|
|
|
use Zend\InputFilter\InputFilterInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class CreatePasswordAction implements MiddlewareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var InputFilterInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $inputFilter; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var LoggerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $logger; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var HydratorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $hydrator; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var EntityManagerRegistry |
|
36
|
|
|
*/ |
|
37
|
|
|
private $managerRegistry; |
|
38
|
|
|
|
|
39
|
8 |
|
public function __construct( |
|
40
|
|
|
EntityManagerRegistry $managerRegistry, |
|
41
|
|
|
InputFilterInterface $inputFilter, |
|
42
|
|
|
LoggerInterface $logger, |
|
43
|
|
|
HydratorInterface $hydrator |
|
44
|
|
|
) { |
|
45
|
8 |
|
$this->managerRegistry = $managerRegistry; |
|
46
|
8 |
|
$this->inputFilter = $inputFilter; |
|
47
|
8 |
|
$this->logger = $logger; |
|
48
|
8 |
|
$this->hydrator = $hydrator; |
|
49
|
8 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
8 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
55
|
|
|
{ |
|
56
|
8 |
|
$data = $request->getParsedBody(); |
|
57
|
8 |
|
if (!is_array($data)) { |
|
58
|
|
|
return (new DataValidationResponseFactory())('password'); |
|
59
|
|
|
} |
|
60
|
8 |
|
$this->inputFilter->setData($data); |
|
61
|
|
|
|
|
62
|
8 |
|
if ($this->inputFilter->isValid()) { |
|
63
|
|
|
try { |
|
64
|
2 |
|
return $this->createPassword($data); |
|
65
|
|
|
} catch (\Exception $exception) { |
|
66
|
|
|
$this->logger->error((string)$exception); |
|
67
|
|
|
return (new GeneralErrorResponseFactory())('There was an error while creating password.', 'password'); |
|
68
|
|
|
} |
|
69
|
|
|
} else { |
|
70
|
6 |
|
return (new ValidationResponseFactory())('password', $this->inputFilter); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $data |
|
76
|
|
|
* @return ResponseInterface |
|
77
|
|
|
* @throws \Exception |
|
78
|
|
|
*/ |
|
79
|
2 |
|
private function createPassword(array $data): ResponseInterface |
|
80
|
|
|
{ |
|
81
|
2 |
|
$data['created_at'] = (new \DateTime())->format(\DateTime::RFC3339); |
|
82
|
2 |
|
$data['due'] = (new \DateTime())->add(new \DateInterval('P1Y'))->format(\DateTime::RFC3339); |
|
83
|
2 |
|
$data['active'] = $data['active'] ?? true; |
|
84
|
2 |
|
$password = $this->hydrator->hydrate($data, new Password()); |
|
85
|
2 |
|
$em = $this->managerRegistry->getManagerForClass(Password::class); |
|
86
|
2 |
|
$em->persist($password); |
|
87
|
2 |
|
$em->flush(); |
|
88
|
2 |
|
$msg = 'Password has been successfully created!'; |
|
89
|
2 |
|
return (new GeneralSuccessResponseFactory())($msg, 'password', $this->hydrator->extract($password)); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|