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

UpdateConfigAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 81.08%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 11
dl 0
loc 87
ccs 30
cts 37
cp 0.8108
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A process() 0 38 5
A getConfig() 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\Doctrine\Middleware\ResourceMiddlewareInterface;
16
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
17
use SlayerBirden\DataFlowServer\Notification\SuccessMessage;
18
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
19
use Zend\Diactoros\Response\JsonResponse;
20
use Zend\Hydrator\ExtractionInterface;
21
use Zend\Hydrator\HydrationInterface;
22
use Zend\InputFilter\InputFilterInterface;
23
24
class UpdateConfigAction implements MiddlewareInterface
25
{
26
    /**
27
     * @var EntityManagerInterface
28
     */
29
    private $entityManager;
30
    /**
31
     * @var ExtractionInterface
32
     */
33
    private $extraction;
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
    /**
39
     * @var HydrationInterface
40
     */
41
    private $hydrator;
42
    /**
43
     * @var InputFilterInterface
44
     */
45
    private $inputFilter;
46
47 2
    public function __construct(
48
        EntityManagerInterface $entityManager,
49
        HydrationInterface $hydrator,
50
        InputFilterInterface $inputFilter,
51
        LoggerInterface $logger,
52
        ExtractionInterface $extraction
53
    ) {
54 2
        $this->entityManager = $entityManager;
55 2
        $this->hydrator = $hydrator;
56 2
        $this->inputFilter = $inputFilter;
57 2
        $this->logger = $logger;
58 2
        $this->extraction = $extraction;
59 2
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
65
    {
66 2
        $data = $request->getParsedBody();
67 2
        $dbConfig = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE);
68 2
        $this->inputFilter->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 66 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...
69
70 2
        $message = null;
71 2
        $updated = false;
72 2
        $status = 200;
73
74 2
        if ($this->inputFilter->isValid()) {
75
            try {
76 1
                $config = $this->getConfig($dbConfig, $data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 66 can also be of type null or object; however, SlayerBirden\DataFlowSer...nfigAction::getConfig() 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...
77 1
                $this->entityManager->persist($config);
78 1
                $this->entityManager->flush();
79 1
                $message = new SuccessMessage('Configuration has been updated!');
80 1
                $updated = true;
81
            } catch (ORMInvalidArgumentException $exception) {
82
                $message = new DangerMessage($exception->getMessage());
83
                $status = 400;
84
            } catch (ORMException $exception) {
85
                $this->logger->error((string)$exception);
86
                $message = new DangerMessage('Error while updating configuration.');
87
                $status = 400;
88
            }
89
        } else {
90 1
            return (new ValidationResponseFactory())('configuration', $this->inputFilter);
91
        }
92
93 1
        return new JsonResponse([
94 1
            'msg' => $message,
95 1
            'success' => $updated,
96
            'data' => [
97 1
                'configuration' => !empty($config) ? $this->extraction->extract($config) : null,
98
                'validation' => [],
99
            ]
100 1
        ], $status);
101
    }
102
103 1
    private function getConfig(DbConfiguration $oldConfig, array $data): DbConfiguration
104
    {
105 1
        unset($data['id']);
106 1
        $this->hydrator->hydrate($data, $oldConfig);
107
108 1
        return $oldConfig;
109
    }
110
}
111