Completed
Push — master ( 088dd2...f9ebf5 )
by Oleg
107:14
created

AddConfigAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 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\Request\Parser;
16
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory;
17
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory;
18
use SlayerBirden\DataFlowServer\Validation\Exception\ValidationException;
19
use Zend\Hydrator\HydratorInterface;
20
21
final class AddConfigAction implements MiddlewareInterface
22
{
23
    /**
24
     * @var HydratorInterface
25
     */
26
    private $hydrator;
27
    /**
28
     * @var LoggerInterface
29
     */
30
    private $logger;
31
    /**
32
     * @var EntityManagerRegistry
33
     */
34
    private $managerRegistry;
35
36 8
    public function __construct(
37
        EntityManagerRegistry $managerRegistry,
38
        HydratorInterface $hydrator,
39
        LoggerInterface $logger
40
    ) {
41 8
        $this->managerRegistry = $managerRegistry;
42 8
        $this->hydrator = $hydrator;
43 8
        $this->logger = $logger;
44 8
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 8
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
50
    {
51 8
        $data = Parser::getRequestBody($request);
52
53
        try {
54 8
            $config = new DbConfiguration();
55 8
            $this->hydrator->hydrate($data, $config);
56 8
            $em = $this->managerRegistry->getManagerForClass(DbConfiguration::class);
57 8
            $em->persist($config);
58 6
            $em->flush();
59 6
            $msg = 'Configuration has been successfully created!';
60 6
            return (new GeneralSuccessResponseFactory())($msg, 'configuration', $this->hydrator->extract($config));
61 2
        } catch (ORMInvalidArgumentException | ValidationException $exception) {
62 2
            return (new GeneralErrorResponseFactory())($exception->getMessage(), 'configuration', 400);
63
        } catch (ORMException $exception) {
64
            $this->logger->error((string)$exception);
65
            return (new GeneralErrorResponseFactory())('Error during creation operation.', 'configuration', 400);
66
        }
67
    }
68
}
69