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\Exception\NonExistingEntity; |
16
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
17
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
18
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
19
|
|
|
use Zend\Hydrator\ExtractionInterface; |
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 ExtractionInterface |
31
|
|
|
*/ |
32
|
|
|
private $extraction; |
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
private $logger; |
37
|
|
|
/** |
38
|
|
|
* @var HydratorInterface |
39
|
|
|
*/ |
40
|
|
|
private $hydrator; |
41
|
|
|
|
42
|
2 |
|
public function __construct( |
43
|
|
|
EntityManagerInterface $entityManager, |
44
|
|
|
HydratorInterface $hydrator, |
45
|
|
|
LoggerInterface $logger, |
46
|
|
|
ExtractionInterface $extraction |
47
|
|
|
) { |
48
|
2 |
|
$this->entityManager = $entityManager; |
49
|
2 |
|
$this->extraction = $extraction; |
50
|
2 |
|
$this->logger = $logger; |
51
|
2 |
|
$this->hydrator = $hydrator; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
2 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
58
|
|
|
{ |
59
|
|
|
// todo check current user |
60
|
2 |
|
$data = $request->getParsedBody(); |
61
|
2 |
|
$id = (int)$request->getAttribute('id'); |
62
|
|
|
|
63
|
2 |
|
$message = null; |
64
|
2 |
|
$updated = false; |
65
|
2 |
|
$status = 200; |
66
|
|
|
|
67
|
|
|
try { |
68
|
2 |
|
$config = $this->getConfig($id, $data); |
|
|
|
|
69
|
|
|
$this->entityManager->persist($config); |
70
|
|
|
$this->entityManager->flush(); |
71
|
|
|
$message = new SuccessMessage('Configuration has been updated!'); |
72
|
|
|
$updated = true; |
73
|
1 |
|
} catch (NonExistingEntity $exception) { |
74
|
|
|
$message = new DangerMessage($exception->getMessage()); |
75
|
|
|
$status = 404; |
76
|
1 |
|
} catch (ORMInvalidArgumentException $exception) { |
77
|
|
|
$message = new DangerMessage($exception->getMessage()); |
78
|
|
|
$status = 400; |
79
|
1 |
|
} catch (ORMException $exception) { |
80
|
|
|
$this->logger->error((string)$exception); |
81
|
|
|
$message = new DangerMessage('Error while updating configuration.'); |
82
|
|
|
$status = 400; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new JsonResponse([ |
86
|
|
|
'msg' => $message, |
87
|
|
|
'success' => $updated, |
88
|
|
|
'data' => [ |
89
|
|
|
'configuration' => !empty($config) ? $this->extraction->extract($config) : null, |
90
|
|
|
] |
91
|
|
|
], $status); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
private function getConfig(int $id, array $data): DbConfiguration |
95
|
|
|
{ |
96
|
|
|
/** @var DbConfiguration $config */ |
97
|
2 |
|
$config = $this->entityManager->find(DbConfiguration::class, $id); |
98
|
1 |
|
if (!$config) { |
99
|
1 |
|
throw new NonExistingEntity(sprintf('Could not find config by id %d.', $data['id'])); |
100
|
|
|
} |
101
|
|
|
if (isset($data['id'])) { |
102
|
|
|
unset($data['id']); |
103
|
|
|
} |
104
|
|
|
// todo set current user as owner |
105
|
|
|
$this->hydrator->hydrate($data, $config); |
106
|
|
|
|
107
|
|
|
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.