Completed
Push — master ( 3eb757...3bc7c8 )
by Oleg
03:46
created

AddUserAction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 75.76%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 12
dl 0
loc 77
ccs 25
cts 33
cp 0.7576
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B process() 0 30 7
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\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\Doctrine\Persistence\EntityManagerRegistry;
15
use SlayerBirden\DataFlowServer\Domain\Entities\User;
16
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory;
17
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory;
18
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory;
19
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
20
use Zend\Hydrator\HydratorInterface;
21
use Zend\InputFilter\InputFilterInterface;
22
23
final class AddUserAction implements MiddlewareInterface
24
{
25
    /**
26
     * @var HydratorInterface
27
     */
28
    private $hydrator;
29
    /**
30
     * @var InputFilterInterface
31
     */
32
    private $inputFilter;
33
    /**
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
    /**
38
     * @var EntityManagerRegistry
39
     */
40
    private $managerRegistry;
41
42 8
    public function __construct(
43
        EntityManagerRegistry $managerRegistry,
44
        HydratorInterface $hydrator,
45
        InputFilterInterface $inputFilter,
46
        LoggerInterface $logger
47
    ) {
48 8
        $this->managerRegistry = $managerRegistry;
49 8
        $this->hydrator = $hydrator;
50 8
        $this->inputFilter = $inputFilter;
51 8
        $this->logger = $logger;
52 8
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 8
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58
    {
59 8
        $data = $request->getParsedBody();
60 8
        if (!is_array($data)) {
61
            return (new DataValidationResponseFactory())('user');
62
        }
63 8
        $this->inputFilter->setData($data);
64
65 8
        if (!$this->inputFilter->isValid()) {
66 4
            return (new ValidationResponseFactory())('user', $this->inputFilter);
67
        }
68
        try {
69 4
            $entity = $this->getEntity($data);
70 4
            $em = $this->managerRegistry->getManagerForClass(User::class);
71 4
            $em->persist($entity);
72 4
            $em->flush();
73 2
            $msg = 'User has been successfully created!';
74 2
            return (new GeneralSuccessResponseFactory())($msg, 'user', $this->hydrator->extract($entity));
75 2
        } catch (ORMInvalidArgumentException $exception) {
76
            return (new GeneralErrorResponseFactory())($exception->getMessage(), 'user', 400);
77 2
        } catch (UniqueConstraintViolationException $exception) {
78 2
            return (new GeneralErrorResponseFactory())('Provided email already exists.', 'user', 400);
79
        } catch (ORMException $exception) {
80
            $this->logger->error((string)$exception);
81
            return (new GeneralErrorResponseFactory())('Error during creation operation.', 'user', 400);
82
        } catch (\Exception $exception) {
83
            $this->logger->error((string)$exception);
84
            return (new GeneralErrorResponseFactory())('Internal error', 'user');
85
        }
86
    }
87
88
    /**
89
     * @param array $data
90
     * @return User
91
     */
92 4
    private function getEntity(array $data): User
93
    {
94 4
        $entity = new User();
95 4
        $this->hydrator->hydrate($data, $entity);
96
97 4
        return $entity;
98
    }
99
}
100