Completed
Push — master ( 5c71f2...630401 )
by Oleg
07:36
created

CreatePasswordAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 0
loc 74
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A process() 0 18 4
A createPassword() 0 15 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authentication\Controller;
5
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Psr\Log\LoggerInterface;
12
use SlayerBirden\DataFlowServer\Authentication\Entities\Password;
13
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory;
14
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory;
15
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory;
16
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
17
use Zend\Hydrator\HydratorInterface;
18
use Zend\InputFilter\InputFilterInterface;
19
20
final class CreatePasswordAction implements MiddlewareInterface
21
{
22
    /**
23
     * @var InputFilterInterface
24
     */
25
    private $inputFilter;
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
    /**
31
     * @var HydratorInterface
32
     */
33
    private $hydrator;
34
    /**
35
     * @var ManagerRegistry
36
     */
37
    private $managerRegistry;
38
39 3
    public function __construct(
40
        ManagerRegistry $managerRegistry,
41
        InputFilterInterface $inputFilter,
42
        LoggerInterface $logger,
43
        HydratorInterface $hydrator
44
    ) {
45 3
        $this->managerRegistry = $managerRegistry;
46 3
        $this->inputFilter = $inputFilter;
47 3
        $this->logger = $logger;
48 3
        $this->hydrator = $hydrator;
49 3
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
55
    {
56 3
        $data = $request->getParsedBody();
57 3
        if (!is_array($data)) {
58
            return (new DataValidationResponseFactory())('password');
59
        }
60 3
        $this->inputFilter->setData($data);
61
62 3
        if ($this->inputFilter->isValid()) {
63
            try {
64 1
                return $this->createPassword($data);
65
            } catch (\Exception $exception) {
66
                return (new GeneralErrorResponseFactory())('There was an error while creating password.', 'password');
67
            }
68
        } else {
69 2
            return (new ValidationResponseFactory())('password', $this->inputFilter);
70
        }
71
    }
72
73
    /**
74
     * @param array $data
75
     * @return ResponseInterface
76
     * @throws \Exception
77
     */
78 1
    private function createPassword(array $data): ResponseInterface
79
    {
80 1
        $data['created_at'] = (new \DateTime())->format(\DateTime::RFC3339);
81 1
        $data['due'] = (new \DateTime())->add(new \DateInterval('P1Y'))->format(\DateTime::RFC3339);
82 1
        $data['active'] = $data['active'] ?? true;
83 1
        $password = $this->hydrator->hydrate($data, new Password());
84 1
        $em = $this->managerRegistry->getManagerForClass(Password::class);
85 1
        if ($em === null) {
86
            throw new \LogicException('Could not retrieve EntityManager.');
87
        }
88 1
        $em->persist($password);
89 1
        $em->flush();
90 1
        $msg = 'Password has been successfully created!';
91 1
        return (new GeneralSuccessResponseFactory())($msg, 'password', $this->hydrator->extract($password));
92
    }
93
}
94