1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Db\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\ORMException; |
8
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use SlayerBirden\DataFlowServer\Db\Entities\DbConfiguration; |
15
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
16
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
17
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
18
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
19
|
|
|
use Zend\Hydrator\HydratorInterface; |
20
|
|
|
use Zend\InputFilter\InputFilterInterface; |
21
|
|
|
|
22
|
|
|
class AddConfigAction implements MiddlewareInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EntityManagerInterface |
26
|
|
|
*/ |
27
|
|
|
private $entityManager; |
28
|
|
|
/** |
29
|
|
|
* @var HydratorInterface |
30
|
|
|
*/ |
31
|
|
|
private $hydrator; |
32
|
|
|
/** |
33
|
|
|
* @var InputFilterInterface |
34
|
|
|
*/ |
35
|
|
|
private $inputFilter; |
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
private $logger; |
40
|
|
|
|
41
|
3 |
|
public function __construct( |
42
|
|
|
EntityManagerInterface $entityManager, |
43
|
|
|
HydratorInterface $hydrator, |
44
|
|
|
InputFilterInterface $inputFilter, |
45
|
|
|
LoggerInterface $logger |
46
|
|
|
) { |
47
|
3 |
|
$this->entityManager = $entityManager; |
48
|
3 |
|
$this->hydrator = $hydrator; |
49
|
3 |
|
$this->inputFilter = $inputFilter; |
50
|
3 |
|
$this->logger = $logger; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
3 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
57
|
|
|
{ |
58
|
3 |
|
$data = $request->getParsedBody(); |
59
|
3 |
|
$this->inputFilter->setData($data); |
|
|
|
|
60
|
|
|
|
61
|
3 |
|
if (!$this->inputFilter->isValid()) { |
62
|
1 |
|
return (new ValidationResponseFactory())('configuration', $this->inputFilter); |
63
|
|
|
} |
64
|
|
|
try { |
65
|
2 |
|
$config = $this->getConfiguration($data); |
|
|
|
|
66
|
2 |
|
$this->entityManager->persist($config); |
67
|
2 |
|
$this->entityManager->flush(); |
68
|
2 |
|
return new JsonResponse([ |
69
|
2 |
|
'msg' => new SuccessMessage('Configuration has been successfully created!'), |
70
|
|
|
'success' => true, |
71
|
|
|
'data' => [ |
72
|
|
|
'validation' => [], |
73
|
2 |
|
'configuration' => $this->hydrator->extract($config), |
74
|
|
|
] |
75
|
2 |
|
], 200); |
76
|
|
|
} catch (ORMInvalidArgumentException $exception) { |
77
|
|
|
return new JsonResponse([ |
78
|
|
|
'msg' => new DangerMessage($exception->getMessage()), |
79
|
|
|
'success' => false, |
80
|
|
|
'data' => [ |
81
|
|
|
'validation' => [], |
82
|
|
|
'configuration' => null, |
83
|
|
|
] |
84
|
|
|
], 400); |
85
|
|
|
} catch (ORMException $exception) { |
86
|
|
|
$this->logger->error((string)$exception); |
87
|
|
|
return new JsonResponse([ |
88
|
|
|
'msg' => new DangerMessage('Error during creation operation.'), |
89
|
|
|
'success' => false, |
90
|
|
|
'data' => [ |
91
|
|
|
'validation' => [], |
92
|
|
|
'configuration' => null, |
93
|
|
|
] |
94
|
|
|
], 500); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $data |
100
|
|
|
* @return DbConfiguration |
101
|
|
|
*/ |
102
|
2 |
|
private function getConfiguration(array $data): DbConfiguration |
103
|
|
|
{ |
104
|
2 |
|
$config = new DbConfiguration(); |
105
|
2 |
|
$this->hydrator->hydrate($data, $config); |
106
|
|
|
|
107
|
2 |
|
return $config; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.