1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Domain\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
16
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
17
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
18
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
19
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
20
|
|
|
use Zend\Hydrator\HydratorInterface; |
21
|
|
|
use Zend\InputFilter\InputFilterInterface; |
22
|
|
|
|
23
|
|
|
class AddUserAction implements MiddlewareInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EntityManagerInterface |
27
|
|
|
*/ |
28
|
|
|
private $entityManager; |
29
|
|
|
/** |
30
|
|
|
* @var HydratorInterface |
31
|
|
|
*/ |
32
|
|
|
private $hydrator; |
33
|
|
|
/** |
34
|
|
|
* @var InputFilterInterface |
35
|
|
|
*/ |
36
|
|
|
private $inputFilter; |
37
|
|
|
/** |
38
|
|
|
* @var LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
private $logger; |
41
|
|
|
|
42
|
4 |
|
public function __construct( |
43
|
|
|
EntityManagerInterface $entityManager, |
44
|
|
|
HydratorInterface $hydrator, |
45
|
|
|
InputFilterInterface $inputFilter, |
46
|
|
|
LoggerInterface $logger |
47
|
|
|
) { |
48
|
4 |
|
$this->entityManager = $entityManager; |
49
|
4 |
|
$this->hydrator = $hydrator; |
50
|
4 |
|
$this->inputFilter = $inputFilter; |
51
|
4 |
|
$this->logger = $logger; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
58
|
|
|
{ |
59
|
4 |
|
$data = $request->getParsedBody(); |
60
|
4 |
|
$this->inputFilter->setData($data); |
|
|
|
|
61
|
|
|
|
62
|
4 |
|
if (!$this->inputFilter->isValid()) { |
63
|
2 |
|
return (new ValidationResponseFactory())('user', $this->inputFilter); |
64
|
|
|
} |
65
|
|
|
try { |
66
|
2 |
|
$entity = $this->getEntity($data); |
|
|
|
|
67
|
2 |
|
$this->entityManager->persist($entity); |
68
|
2 |
|
$this->entityManager->flush(); |
69
|
1 |
|
return new JsonResponse([ |
70
|
1 |
|
'msg' => new SuccessMessage('User has been successfully created!'), |
71
|
|
|
'success' => true, |
72
|
|
|
'data' => [ |
73
|
|
|
'validation' => [], |
74
|
1 |
|
'user' => $this->hydrator->extract($entity), |
75
|
|
|
] |
76
|
1 |
|
], 200); |
77
|
1 |
|
} catch (ORMInvalidArgumentException $exception) { |
78
|
|
|
return new JsonResponse([ |
79
|
|
|
'msg' => new DangerMessage($exception->getMessage()), |
80
|
|
|
'success' => false, |
81
|
|
|
'data' => [ |
82
|
|
|
'validation' => [], |
83
|
|
|
'user' => null, |
84
|
|
|
] |
85
|
|
|
], 400); |
86
|
1 |
|
} catch (UniqueConstraintViolationException $exception) { |
87
|
1 |
|
return new JsonResponse([ |
88
|
1 |
|
'msg' => new DangerMessage('Provided email already exists.'), |
89
|
|
|
'success' => false, |
90
|
|
|
'data' => [ |
91
|
|
|
'validation' => [], |
92
|
|
|
'user' => null, |
93
|
|
|
] |
94
|
1 |
|
], 400); |
95
|
|
|
} catch (ORMException $exception) { |
96
|
|
|
$this->logger->error((string)$exception); |
97
|
|
|
return new JsonResponse([ |
98
|
|
|
'msg' => new DangerMessage('Error during creation operation.'), |
99
|
|
|
'success' => false, |
100
|
|
|
'data' => [ |
101
|
|
|
'validation' => [], |
102
|
|
|
'user' => null, |
103
|
|
|
] |
104
|
|
|
], 400); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $data |
110
|
|
|
* @return User |
111
|
|
|
*/ |
112
|
2 |
|
private function getEntity(array $data): User |
113
|
|
|
{ |
114
|
2 |
|
$entity = new User(); |
115
|
2 |
|
$this->hydrator->hydrate($data, $entity); |
116
|
|
|
|
117
|
2 |
|
return $entity; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.