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\GeneralSuccessResponseFactory; |
15
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
16
|
|
|
use Zend\Hydrator\HydratorInterface; |
17
|
|
|
use Zend\InputFilter\InputFilterInterface; |
18
|
|
|
|
19
|
|
|
final class CreatePasswordAction implements MiddlewareInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var InputFilterInterface |
23
|
|
|
*/ |
24
|
|
|
private $inputFilter; |
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
/** |
30
|
|
|
* @var HydratorInterface |
31
|
|
|
*/ |
32
|
|
|
private $hydrator; |
33
|
|
|
/** |
34
|
|
|
* @var EntityManagerRegistry |
35
|
|
|
*/ |
36
|
|
|
private $managerRegistry; |
37
|
|
|
|
38
|
8 |
|
public function __construct( |
39
|
|
|
EntityManagerRegistry $managerRegistry, |
40
|
|
|
InputFilterInterface $inputFilter, |
41
|
|
|
LoggerInterface $logger, |
42
|
|
|
HydratorInterface $hydrator |
43
|
|
|
) { |
44
|
8 |
|
$this->managerRegistry = $managerRegistry; |
45
|
8 |
|
$this->inputFilter = $inputFilter; |
46
|
8 |
|
$this->logger = $logger; |
47
|
8 |
|
$this->hydrator = $hydrator; |
48
|
8 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
* @throws \Exception |
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
|
2 |
|
return $this->createPassword($data); |
64
|
|
|
} else { |
65
|
6 |
|
return (new ValidationResponseFactory())('password', $this->inputFilter); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $data |
71
|
|
|
* @return ResponseInterface |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
2 |
|
private function createPassword(array $data): ResponseInterface |
75
|
|
|
{ |
76
|
2 |
|
$data['created_at'] = (new \DateTime())->format(\DateTime::RFC3339); |
77
|
2 |
|
$data['due'] = (new \DateTime())->add(new \DateInterval('P1Y'))->format(\DateTime::RFC3339); |
78
|
2 |
|
$data['active'] = $data['active'] ?? true; |
79
|
2 |
|
$password = $this->hydrator->hydrate($data, new Password()); |
80
|
2 |
|
$em = $this->managerRegistry->getManagerForClass(Password::class); |
81
|
2 |
|
$em->persist($password); |
82
|
2 |
|
$em->flush(); |
83
|
2 |
|
$msg = 'Password has been successfully created!'; |
84
|
2 |
|
return (new GeneralSuccessResponseFactory())($msg, 'password', $this->hydrator->extract($password)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|