Completed
Push — master ( 3bc7c8...a199f7 )
by Oleg
03:54
created

AddConfigAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 12
dl 0
loc 72
ccs 22
cts 28
cp 0.7856
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A process() 0 25 5
A getConfiguration() 0 7 1
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\Persistence\EntityManagerRegistry;
15
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory;
16
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory;
17
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory;
18
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
19
use Zend\Hydrator\HydratorInterface;
20
use Zend\InputFilter\InputFilterInterface;
21
22
final class AddConfigAction implements MiddlewareInterface
23
{
24
    /**
25
     * @var HydratorInterface
26
     */
27
    private $hydrator;
28
    /**
29
     * @var InputFilterInterface
30
     */
31
    private $inputFilter;
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
    /**
37
     * @var EntityManagerRegistry
38
     */
39
    private $managerRegistry;
40
41 6
    public function __construct(
42
        EntityManagerRegistry $managerRegistry,
43
        HydratorInterface $hydrator,
44
        InputFilterInterface $inputFilter,
45
        LoggerInterface $logger
46
    ) {
47 6
        $this->managerRegistry = $managerRegistry;
48 6
        $this->hydrator = $hydrator;
49 6
        $this->inputFilter = $inputFilter;
50 6
        $this->logger = $logger;
51 6
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 6
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58 6
        $data = $request->getParsedBody();
59 6
        if (!is_array($data)) {
60
            return (new DataValidationResponseFactory())('configuration');
61
        }
62 6
        $this->inputFilter->setData($data);
63
64 6
        if (!$this->inputFilter->isValid()) {
65 2
            return (new ValidationResponseFactory())('configuration', $this->inputFilter);
66
        }
67
        try {
68 4
            $config = $this->getConfiguration($data);
69 4
            $em = $this->managerRegistry->getManagerForClass(DbConfiguration::class);
70 4
            $em->persist($config);
71 4
            $em->flush();
72 4
            $msg = 'Configuration has been successfully created!';
73 4
            return (new GeneralSuccessResponseFactory())($msg, 'configuration', $this->hydrator->extract($config));
74
        } catch (ORMInvalidArgumentException $exception) {
75
            return (new GeneralErrorResponseFactory())($exception->getMessage(), 'configuration', 400);
76
        } catch (ORMException $exception) {
77
            $this->logger->error((string)$exception);
78
            return (new GeneralErrorResponseFactory())('Error during creation operation.', 'configuration', 400);
79
        }
80
    }
81
82
    /**
83
     * @param array $data
84
     * @return DbConfiguration
85
     */
86 4
    private function getConfiguration(array $data): DbConfiguration
87
    {
88 4
        $config = new DbConfiguration();
89 4
        $this->hydrator->hydrate($data, $config);
90
91 4
        return $config;
92
    }
93
}
94