Completed
Push — master ( 178a08...5c0b6f )
by Oleg
05:22
created

AddUserAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 77.14%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 11
dl 0
loc 97
ccs 27
cts 35
cp 0.7714
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B process() 0 50 5
A getEntity() 0 7 1
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\HydratorInterface;
21
use Zend\InputFilter\InputFilterInterface;
22
23
class AddUserAction implements MiddlewareInterface
24
{
25
    /**
26
     * @var EntityManagerInterface
27
     */
28
    private $entityManager;
29
    /**
30
     * @var HydratorInterface
31
     */
32
    private $hydrator;
33
    /**
34
     * @var InputFilterInterface
35
     */
36
    private $inputFilter;
37
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
42 4
    public function __construct(
43
        EntityManagerInterface $entityManager,
44
        HydratorInterface $hydrator,
45
        InputFilterInterface $inputFilter,
46
        LoggerInterface $logger
47
    ) {
48 4
        $this->entityManager = $entityManager;
49 4
        $this->hydrator = $hydrator;
50 4
        $this->inputFilter = $inputFilter;
51 4
        $this->logger = $logger;
52 4
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58
    {
59 4
        $data = $request->getParsedBody();
60 4
        $this->inputFilter->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 59 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...
61
62 4
        if (!$this->inputFilter->isValid()) {
63 2
            return (new ValidationResponseFactory())('user', $this->inputFilter);
64
        }
65
        try {
66 2
            $entity = $this->getEntity($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 59 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...
67 2
            $this->entityManager->persist($entity);
68 2
            $this->entityManager->flush();
69 1
            return new JsonResponse([
70 1
                'msg' => new SuccessMessage('User has been successfully created!'),
71
                'success' => true,
72
                'data' => [
73
                    'validation' => [],
74 1
                    'user' => $this->hydrator->extract($entity),
75
                ]
76 1
            ], 200);
77 1
        } catch (ORMInvalidArgumentException $exception) {
78
            return new JsonResponse([
79
                'msg' => new DangerMessage($exception->getMessage()),
80
                'success' => false,
81
                'data' => [
82
                    'validation' => [],
83
                    'user' => null,
84
                ]
85
            ], 400);
86 1
        } catch (UniqueConstraintViolationException $exception) {
87 1
            return new JsonResponse([
88 1
                'msg' => new DangerMessage('Provided email already exists.'),
89
                'success' => false,
90
                'data' => [
91
                    'validation' => [],
92
                    'user' => null,
93
                ]
94 1
            ], 400);
95
        } catch (ORMException $exception) {
96
            $this->logger->error((string)$exception);
97
            return new JsonResponse([
98
                'msg' => new DangerMessage('Error during creation operation.'),
99
                'success' => false,
100
                'data' => [
101
                    'validation' => [],
102
                    'user' => null,
103
                ]
104
            ], 400);
105
        }
106
    }
107
108
    /**
109
     * @param array $data
110
     * @return User
111
     */
112 2
    private function getEntity(array $data): User
113
    {
114 2
        $entity = new User();
115 2
        $this->hydrator->hydrate($data, $entity);
116
117 2
        return $entity;
118
    }
119
}
120