1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
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\ValidationResponseFactory; |
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
17
|
|
|
use Zend\Hydrator\HydratorInterface; |
18
|
|
|
use Zend\InputFilter\InputFilterInterface; |
19
|
|
|
|
20
|
|
|
class CreatePasswordAction implements MiddlewareInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var EntityManager |
24
|
|
|
*/ |
25
|
|
|
private $entityManager; |
26
|
|
|
/** |
27
|
|
|
* @var InputFilterInterface |
28
|
|
|
*/ |
29
|
|
|
private $inputFilter; |
30
|
|
|
/** |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
private $logger; |
34
|
|
|
/** |
35
|
|
|
* @var HydratorInterface |
36
|
|
|
*/ |
37
|
|
|
private $hydrator; |
38
|
|
|
|
39
|
3 |
|
public function __construct( |
40
|
|
|
EntityManager $entityManager, |
|
|
|
|
41
|
|
|
InputFilterInterface $inputFilter, |
42
|
|
|
LoggerInterface $logger, |
43
|
|
|
HydratorInterface $hydrator |
44
|
|
|
) { |
45
|
3 |
|
$this->entityManager = $entityManager; |
46
|
3 |
|
$this->inputFilter = $inputFilter; |
47
|
3 |
|
$this->logger = $logger; |
48
|
3 |
|
$this->hydrator = $hydrator; |
49
|
3 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
3 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
55
|
|
|
{ |
56
|
3 |
|
$data = $request->getParsedBody(); |
57
|
3 |
|
$this->inputFilter->setData($data); |
|
|
|
|
58
|
|
|
|
59
|
3 |
|
if ($this->inputFilter->isValid()) { |
60
|
|
|
try { |
61
|
1 |
|
return $this->createPassword($data); |
|
|
|
|
62
|
|
|
} catch (\Exception $exception) { |
63
|
|
|
return new JsonResponse([ |
64
|
|
|
'msg' => new DangerMessage('There was an error while creating password.'), |
65
|
|
|
'success' => true, |
66
|
|
|
'data' => [ |
67
|
|
|
'validation' => [], |
68
|
|
|
'password' => null, |
69
|
|
|
] |
70
|
|
|
], 500); |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
2 |
|
return (new ValidationResponseFactory())('password', $this->inputFilter); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $data |
79
|
|
|
* @return ResponseInterface |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
1 |
|
private function createPassword(array $data): ResponseInterface |
83
|
|
|
{ |
84
|
1 |
|
$data['created_at'] = (new \DateTime())->format(\DateTime::RFC3339); |
85
|
1 |
|
$data['due'] = (new \DateTime())->add(new \DateInterval('P1Y'))->format(\DateTime::RFC3339); |
86
|
1 |
|
$data['active'] = $data['active'] ?? true; |
87
|
1 |
|
$password = $this->hydrator->hydrate($data, new Password()); |
88
|
1 |
|
$this->entityManager->persist($password); |
89
|
1 |
|
$this->entityManager->flush(); |
90
|
1 |
|
return new JsonResponse([ |
91
|
1 |
|
'msg' => new SuccessMessage('Password has been successfully created!'), |
92
|
|
|
'success' => true, |
93
|
|
|
'data' => [ |
94
|
|
|
'validation' => [], |
95
|
1 |
|
'password' => $this->hydrator->extract($password), |
96
|
|
|
] |
97
|
1 |
|
], 200); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.