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

PasswordHydrator::hydrate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 15
cp 0.8
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 4
nop 2
crap 5.2
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