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