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