Completed
Push — master ( 3eb757...3bc7c8 )
by Oleg
03:46
created

UpdateConfigAction::process()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.4648

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 13
cts 22
cp 0.5909
rs 8.8337
c 0
b 0
f 0
cc 6
nc 18
nop 2
crap 8.4648
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\Validation\DataValidationResponseFactory;
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 = $request->getParsedBody();
60 4
        if (!is_array($data)) {
61
            return (new DataValidationResponseFactory())('configuration');
62
        }
63 4
        $dbConfig = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE);
64 4
        $this->inputFilter->setData($data);
65
66 4
        if (!$this->inputFilter->isValid()) {
67 2
            return (new ValidationResponseFactory())('configuration', $this->inputFilter);
68
        }
69
        try {
70 2
            $config = $this->getConfig($dbConfig, $data);
71 2
            $em = $this->managerRegistry->getManagerForClass(DbConfiguration::class);
72 2
            $em->persist($config);
73 2
            $em->flush();
74 2
            $msg = 'Configuration has been updated!';
75 2
            return (new GeneralSuccessResponseFactory())($msg, 'configuration', $this->hydrator->extract($config));
76
        } catch (ORMInvalidArgumentException $exception) {
77
            return (new GeneralErrorResponseFactory())($exception->getMessage(), 'configuration', 400);
78
        } catch (ORMException $exception) {
79
            $this->logger->error((string)$exception);
80
            return (new GeneralErrorResponseFactory())('Error while updating configuration.', 'configuration', 400);
81
        } catch (\Exception $exception) {
82
            $this->logger->error((string)$exception);
83
            return (new GeneralErrorResponseFactory())('Internal error', 'configuration');
84
        }
85
    }
86
87 2
    private function getConfig(DbConfiguration $configuration, array $data): DbConfiguration
88
    {
89 2
        unset($data['id']);
90 2
        $this->hydrator->hydrate($data, $configuration);
91
92 2
        return $configuration;
93
    }
94
}
95