Completed
Push — master ( 298ac7...0024da )
by Oleg
12:58
created

AddConfigAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 84.09%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 12
dl 0
loc 99
ccs 37
cts 44
cp 0.8409
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B process() 0 46 6
A getConfiguration() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Db\Controller;
5
6
use Doctrine\ORM\EntityManagerInterface;
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\Notification\DangerMessage;
16
use SlayerBirden\DataFlowServer\Notification\SuccessMessage;
17
use Zend\Diactoros\Response\JsonResponse;
18
use Zend\Hydrator\ExtractionInterface;
19
use Zend\Hydrator\HydrationInterface;
20
use Zend\InputFilter\InputFilterInterface;
21
22
class AddConfigAction implements MiddlewareInterface
23
{
24
    /**
25
     * @var EntityManagerInterface
26
     */
27
    private $entityManager;
28
    /**
29
     * @var HydrationInterface
30
     */
31
    private $hydrator;
32
    /**
33
     * @var InputFilterInterface
34
     */
35
    private $inputFilter;
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
    /**
41
     * @var ExtractionInterface
42
     */
43
    private $extractor;
44
45 3
    public function __construct(
46
        EntityManagerInterface $entityManager,
47
        HydrationInterface $hydrator,
48
        InputFilterInterface $inputFilter,
49
        LoggerInterface $logger,
50
        ExtractionInterface $extractor
51
    ) {
52 3
        $this->entityManager = $entityManager;
53 3
        $this->hydrator = $hydrator;
54 3
        $this->inputFilter = $inputFilter;
55 3
        $this->logger = $logger;
56 3
        $this->extractor = $extractor;
57 3
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
63
    {
64 3
        $data = $request->getParsedBody();
65 3
        $this->inputFilter->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 64 can also be of type null or object; however, Zend\InputFilter\InputFilterInterface::setData() does only seem to accept array|object<Traversable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
67 3
        $message = null;
68 3
        $validation = [];
69 3
        $created = false;
70 3
        $status = 200;
71
72 3
        if ($this->inputFilter->isValid()) {
73
            try {
74 2
                $config = $this->getConfiguration($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 64 can also be of type null or object; however, SlayerBirden\DataFlowSer...ion::getConfiguration() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75 2
                $this->entityManager->persist($config);
76 2
                $this->entityManager->flush();
77 2
                $message = new SuccessMessage('Configuration has been successfully created!');
78 2
                $created = true;
79
            } catch (ORMInvalidArgumentException $exception) {
80
                $message = new DangerMessage($exception->getMessage());
81
                $status = 400;
82
            } catch (ORMException $exception) {
83
                $this->logger->error((string)$exception);
84
                $message = new DangerMessage('Error during creation operation.');
85
                $status = 400;
86
            }
87
        } else {
88 1
            $message = new DangerMessage('There were validation errors.');
89 1
            foreach ($this->inputFilter->getInvalidInput() as $key => $input) {
90 1
                $messages = $input->getMessages();
91 1
                $validation[] = [
92 1
                    'field' => $key,
93 1
                    'msg' => reset($messages)
94
                ];
95
            }
96 1
            $status = 400;
97
        }
98
99 3
        return new JsonResponse([
100 3
            'msg' => $message,
101 3
            'success' => $created,
102
            'data' => [
103 3
                'validation' => $validation,
104 2
                'configuration' => !empty($config) ? $this->extractor->extract($config) : null,
105
            ]
106 3
        ], $status);
107
    }
108
109
    /**
110
     * @param array $data
111
     * @return DbConfiguration
112
     */
113 2
    private function getConfiguration(array $data): DbConfiguration
114
    {
115 2
        $config = new DbConfiguration();
116 2
        $this->hydrator->hydrate($data, $config);
117
118 2
        return $config;
119
    }
120
}
121