Completed
Push — master ( 0024da...3d898f )
by Oleg
03:17
created

AddConfigAction::process()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.5488

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 18
cts 25
cp 0.72
rs 9.0168
c 0
b 0
f 0
cc 5
nc 10
nop 2
crap 5.5488
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 SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
18
use Zend\Diactoros\Response\JsonResponse;
19
use Zend\Hydrator\ExtractionInterface;
20
use Zend\Hydrator\HydrationInterface;
21
use Zend\InputFilter\InputFilterInterface;
22
23
class AddConfigAction implements MiddlewareInterface
24
{
25
    /**
26
     * @var EntityManagerInterface
27
     */
28
    private $entityManager;
29
    /**
30
     * @var HydrationInterface
31
     */
32
    private $hydrator;
33
    /**
34
     * @var InputFilterInterface
35
     */
36
    private $inputFilter;
37
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
    /**
42
     * @var ExtractionInterface
43
     */
44
    private $extractor;
45
46 3
    public function __construct(
47
        EntityManagerInterface $entityManager,
48
        HydrationInterface $hydrator,
49
        InputFilterInterface $inputFilter,
50
        LoggerInterface $logger,
51
        ExtractionInterface $extractor
52
    ) {
53 3
        $this->entityManager = $entityManager;
54 3
        $this->hydrator = $hydrator;
55 3
        $this->inputFilter = $inputFilter;
56 3
        $this->logger = $logger;
57 3
        $this->extractor = $extractor;
58 3
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64
    {
65 3
        $data = $request->getParsedBody();
66 3
        $this->inputFilter->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 65 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...
67
68 3
        $message = null;
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 65 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 = 500;
86
            }
87
        } else {
88 1
            return (new ValidationResponseFactory())('configuration', $this->inputFilter);
89
        }
90
91 2
        return new JsonResponse([
92 2
            'msg' => $message,
93 2
            'success' => $created,
94
            'data' => [
95
                'validation' => [],
96 2
                'configuration' => !empty($config) ? $this->extractor->extract($config) : null,
97
            ]
98 2
        ], $status);
99
    }
100
101
    /**
102
     * @param array $data
103
     * @return DbConfiguration
104
     */
105 2
    private function getConfiguration(array $data): DbConfiguration
106
    {
107 2
        $config = new DbConfiguration();
108 2
        $this->hydrator->hydrate($data, $config);
109
110 2
        return $config;
111
    }
112
}
113