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