Completed
Push — master ( 2ef37c...52cad6 )
by Oleg
02:13
created

UpdateConfigAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 4
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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\Exception\NonExistingEntity;
16
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
17
use SlayerBirden\DataFlowServer\Notification\SuccessMessage;
18
use Zend\Diactoros\Response\JsonResponse;
19
use Zend\Hydrator\ExtractionInterface;
20
use Zend\Hydrator\HydratorInterface;
21
use Zend\InputFilter\InputFilterInterface;
22
23
class UpdateConfigAction implements MiddlewareInterface
24
{
25
    /**
26
     * @var EntityManagerInterface
27
     */
28
    private $entityManager;
29
    /**
30
     * @var ExtractionInterface
31
     */
32
    private $extraction;
33
    /**
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
    /**
38
     * @var HydratorInterface
39
     */
40
    private $hydrator;
41
42 2
    public function __construct(
43
        EntityManagerInterface $entityManager,
44
        HydratorInterface $hydrator,
45
        LoggerInterface $logger,
46
        ExtractionInterface $extraction
47
    ) {
48 2
        $this->entityManager = $entityManager;
49 2
        $this->extraction = $extraction;
50 2
        $this->logger = $logger;
51 2
        $this->hydrator = $hydrator;
52 2
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58
    {
59
        // todo check current user
60 2
        $data = $request->getParsedBody();
61 2
        $id = (int)$request->getAttribute('id');
62
63 2
        $message = null;
64 2
        $updated = false;
65 2
        $status = 200;
66
67
        try {
68 2
            $config = $this->getConfig($id, $data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 60 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...
69
            $this->entityManager->persist($config);
70
            $this->entityManager->flush();
71
            $message = new SuccessMessage('Configuration has been updated!');
72
            $updated = true;
73 1
        } catch (NonExistingEntity $exception) {
74
            $message = new DangerMessage($exception->getMessage());
75
            $status = 404;
76 1
        } catch (ORMInvalidArgumentException $exception) {
77
            $message = new DangerMessage($exception->getMessage());
78
            $status = 400;
79 1
        } catch (ORMException $exception) {
80
            $this->logger->error((string)$exception);
81
            $message = new DangerMessage('Error while updating configuration.');
82
            $status = 400;
83
        }
84
85
        return new JsonResponse([
86
            'msg' => $message,
87
            'success' => $updated,
88
            'data' => [
89
                'configuration' => !empty($config) ? $this->extraction->extract($config) : null,
90
            ]
91
        ], $status);
92
    }
93
94 2
    private function getConfig(int $id, array $data): DbConfiguration
95
    {
96
        /** @var DbConfiguration $config */
97 2
        $config = $this->entityManager->find(DbConfiguration::class, $id);
98 1
        if (!$config) {
99 1
            throw new NonExistingEntity(sprintf('Could not find config by id %d.', $data['id']));
100
        }
101
        if (isset($data['id'])) {
102
            unset($data['id']);
103
        }
104
        // todo set current user as owner
105
        $this->hydrator->hydrate($data, $config);
106
107
        return $config;
108
    }
109
}
110