1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Db\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\ORMException; |
7
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use SlayerBirden\DataFlowServer\Db\Entities\DbConfiguration; |
14
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface; |
15
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry; |
16
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Request\Parser; |
17
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory; |
18
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory; |
19
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory; |
20
|
|
|
use Zend\Hydrator\HydratorInterface; |
21
|
|
|
use Zend\InputFilter\InputFilterInterface; |
22
|
|
|
|
23
|
|
|
final class UpdateConfigAction implements MiddlewareInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
/** |
30
|
|
|
* @var HydratorInterface |
31
|
|
|
*/ |
32
|
|
|
private $hydrator; |
33
|
|
|
/** |
34
|
|
|
* @var InputFilterInterface |
35
|
|
|
*/ |
36
|
|
|
private $inputFilter; |
37
|
|
|
/** |
38
|
|
|
* @var EntityManagerRegistry |
39
|
|
|
*/ |
40
|
|
|
private $managerRegistry; |
41
|
|
|
|
42
|
4 |
|
public function __construct( |
43
|
|
|
EntityManagerRegistry $managerRegistry, |
44
|
|
|
HydratorInterface $hydrator, |
45
|
|
|
InputFilterInterface $inputFilter, |
46
|
|
|
LoggerInterface $logger |
47
|
|
|
) { |
48
|
4 |
|
$this->managerRegistry = $managerRegistry; |
49
|
4 |
|
$this->hydrator = $hydrator; |
50
|
4 |
|
$this->inputFilter = $inputFilter; |
51
|
4 |
|
$this->logger = $logger; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
58
|
|
|
{ |
59
|
4 |
|
$data = Parser::getRequestBody($request); |
60
|
4 |
|
$dbConfig = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE); |
61
|
4 |
|
$this->inputFilter->setData($data); |
62
|
|
|
|
63
|
4 |
|
if (!$this->inputFilter->isValid()) { |
64
|
2 |
|
return (new ValidationResponseFactory())('configuration', $this->inputFilter); |
65
|
|
|
} |
66
|
|
|
try { |
67
|
2 |
|
$this->hydrator->hydrate($data, $dbConfig); |
68
|
2 |
|
$em = $this->managerRegistry->getManagerForClass(DbConfiguration::class); |
69
|
2 |
|
$em->persist($dbConfig); |
70
|
2 |
|
$em->flush(); |
71
|
2 |
|
$msg = 'Configuration has been updated!'; |
72
|
2 |
|
return (new GeneralSuccessResponseFactory())($msg, 'configuration', $this->hydrator->extract($dbConfig)); |
73
|
|
|
} catch (ORMInvalidArgumentException $exception) { |
74
|
|
|
return (new GeneralErrorResponseFactory())($exception->getMessage(), 'configuration', 400); |
75
|
|
|
} catch (ORMException $exception) { |
76
|
|
|
$this->logger->error((string)$exception); |
77
|
|
|
return (new GeneralErrorResponseFactory())('Error while updating configuration.', 'configuration', 400); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|