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