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