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

AddUserAction::process()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.3541

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 22
cts 28
cp 0.7856
rs 8.6577
c 0
b 0
f 0
cc 6
nc 14
nop 2
crap 6.3541
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Domain\Controller;
5
6
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\ORMException;
9
use Doctrine\ORM\ORMInvalidArgumentException;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\MiddlewareInterface;
13
use Psr\Http\Server\RequestHandlerInterface;
14
use Psr\Log\LoggerInterface;
15
use SlayerBirden\DataFlowServer\Domain\Entities\User;
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\HydratorInterface;
22
use Zend\InputFilter\InputFilterInterface;
23
24
class AddUserAction implements MiddlewareInterface
25
{
26
    /**
27
     * @var EntityManagerInterface
28
     */
29
    private $entityManager;
30
    /**
31
     * @var HydratorInterface
32
     */
33
    private $hydrator;
34
    /**
35
     * @var InputFilterInterface
36
     */
37
    private $inputFilter;
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
    /**
43
     * @var ExtractionInterface
44
     */
45
    private $extraction;
46
47 4
    public function __construct(
48
        EntityManagerInterface $entityManager,
49
        HydratorInterface $hydrator,
50
        InputFilterInterface $inputFilter,
51
        LoggerInterface $logger,
52
        ExtractionInterface $extraction
53
    ) {
54 4
        $this->entityManager = $entityManager;
55 4
        $this->hydrator = $hydrator;
56 4
        $this->inputFilter = $inputFilter;
57 4
        $this->logger = $logger;
58 4
        $this->extraction = $extraction;
59 4
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
65
    {
66 4
        $data = $request->getParsedBody();
67 4
        $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...
68
69 4
        $message = null;
70 4
        $created = false;
71 4
        $status = 200;
72
73 4
        if ($this->inputFilter->isValid()) {
74
            try {
75 2
                $entity = $this->getEntity($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...UserAction::getEntity() 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...
76 2
                $this->entityManager->persist($entity);
77 2
                $this->entityManager->flush();
78 1
                $message = new SuccessMessage('User has been successfully created!');
79 1
                $created = true;
80 1
            } catch (ORMInvalidArgumentException $exception) {
81
                $message = new DangerMessage($exception->getMessage());
82
                $status = 400;
83 1
            } catch (UniqueConstraintViolationException $exception) {
84 1
                $message = new DangerMessage('Provided email already exists.');
85 1
                $status = 400;
86
            } catch (ORMException $exception) {
87
                $this->logger->error((string)$exception);
88
                $message = new DangerMessage('Error during creation operation.');
89
                $status = 400;
90
            }
91
        } else {
92 2
            return (new ValidationResponseFactory())('user', $this->inputFilter);
93
        }
94
95 2
        return new JsonResponse([
96 2
            'msg' => $message,
97 2
            'success' => $created,
98
            'data' => [
99
                'validation' => [],
100 2
                'user' => isset($entity) ? $this->extraction->extract($entity) : null,
101
            ]
102 2
        ], $status);
103
    }
104
105
    /**
106
     * @param array $data
107
     * @return User
108
     */
109 2
    private function getEntity(array $data): User
110
    {
111 2
        $entity = new User();
112 2
        $this->hydrator->hydrate($data, $entity);
113
114 2
        return $entity;
115
    }
116
}
117