Completed
Push — master ( 298ac7...0024da )
by Oleg
12:58
created

PasswordHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 41
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B hydrate() 0 24 5
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authentication\Hydrator;
5
6
use SlayerBirden\DataFlowServer\Authentication\Entities\Password;
7
use SlayerBirden\DataFlowServer\Authentication\PasswordManagerInterface;
8
use Zend\Hydrator\HydrationInterface;
9
10
class PasswordHydrator implements HydrationInterface
11
{
12
    /**
13
     * @var PasswordManagerInterface
14
     */
15
    private $passwordManager;
16
17 4
    public function __construct(PasswordManagerInterface $passwordManager)
18
    {
19 4
        $this->passwordManager = $passwordManager;
20 4
    }
21
22
    /**
23
     * @inheritdoc
24
     * @throws \Exception
25
     */
26 1
    public function hydrate(array $data, $object): Password
27
    {
28 1
        if (empty($data['password'])) {
29
            throw new \InvalidArgumentException('Can not find password among the data.');
30
        }
31 1
        if (empty($data['owner'])) {
32
            throw new \InvalidArgumentException('No user provided. Can not set password without a user.');
33
        }
34 1
        if (!($object instanceof Password)) {
35
            throw new \InvalidArgumentException('Only Password object can be hydrated by this service.');
36
        }
37
38 1
        $now = new \DateTime();
39
        // password due in 1 Year
40 1
        $due = (new \DateTime())->add(new \DateInterval('P1Y'));
41
42 1
        $object->setActive(isset($data['active']) ? (bool)$data['active'] : true);
43 1
        $object->setCreatedAt($now);
44 1
        $object->setDue($due);
45 1
        $object->setOwner($data['owner']);
46 1
        $object->setHash($this->passwordManager->getHash($data['password']));
47
48 1
        return $object;
49
    }
50
}
51