Completed
Push — master ( 5c0b6f...5c71f2 )
by Oleg
10:16
created

UpdateConfigAction::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Db\Controller;
5
6
use Doctrine\Common\Persistence\ManagerRegistry;
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\DataValidationResponseFactory;
19
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
20
use Zend\Diactoros\Response\JsonResponse;
21
use Zend\Hydrator\HydratorInterface;
22
use Zend\InputFilter\InputFilterInterface;
23
24
final class UpdateConfigAction implements MiddlewareInterface
25
{
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
    /**
31
     * @var HydratorInterface
32
     */
33
    private $hydrator;
34
    /**
35
     * @var InputFilterInterface
36
     */
37
    private $inputFilter;
38
    /**
39
     * @var ManagerRegistry
40
     */
41
    private $managerRegistry;
42
43 2
    public function __construct(
44
        ManagerRegistry $managerRegistry,
45
        HydratorInterface $hydrator,
46
        InputFilterInterface $inputFilter,
47
        LoggerInterface $logger
48
    ) {
49 2
        $this->managerRegistry = $managerRegistry;
50 2
        $this->hydrator = $hydrator;
51 2
        $this->inputFilter = $inputFilter;
52 2
        $this->logger = $logger;
53 2
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
59
    {
60 2
        $data = $request->getParsedBody();
61 2
        if (!is_array($data)) {
62
            return (new DataValidationResponseFactory())('configuration');
63
        }
64 2
        $dbConfig = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE);
65 2
        $this->inputFilter->setData($data);
66
67 2
        if (!$this->inputFilter->isValid()) {
68 1
            return (new ValidationResponseFactory())('configuration', $this->inputFilter);
69
        }
70
        try {
71 1
            $config = $this->getConfig($dbConfig, $data);
72 1
            $em = $this->managerRegistry->getManagerForClass(DbConfiguration::class);
73 1
            $em->persist($config);
74 1
            $em->flush();
75 1
            return new JsonResponse([
76 1
                'msg' => new SuccessMessage('Configuration has been updated!'),
77
                'success' => true,
78
                'data' => [
79 1
                    'configuration' => $this->hydrator->extract($config),
80
                    'validation' => [],
81
                ]
82 1
            ], 200);
83
        } catch (ORMInvalidArgumentException $exception) {
84
            return new JsonResponse([
85
                'msg' => new DangerMessage($exception->getMessage()),
86
                'success' => false,
87
                'data' => [
88
                    'configuration' => null,
89
                    'validation' => [],
90
                ]
91
            ], 400);
92
        } catch (ORMException $exception) {
93
            $this->logger->error((string)$exception);
94
            return new JsonResponse([
95
                'msg' => new DangerMessage('Error while updating configuration.'),
96
                'success' => false,
97
                'data' => [
98
                    'configuration' => null,
99
                    'validation' => [],
100
                ]
101
            ], 400);
102
        }
103
    }
104
105 1
    private function getConfig(DbConfiguration $configuration, array $data): DbConfiguration
106
    {
107 1
        unset($data['id']);
108 1
        $this->hydrator->hydrate($data, $configuration);
109
110 1
        return $configuration;
111
    }
112
}
113