Passed
Push — master ( d57cd8...52a71d )
by Mārtiņš
03:14
created

Mapper::store()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
c 0
b 0
f 0
rs 9.8333
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Ignis\Mapper;
4
5
use Memcached;
6
use Ignis\Entity;
7
use Ignis\Exception;
8
9
class Mapper
10
{
11
    private $storage;
12
    private $lifetime;
13
    private $burndown;
14
15
    public function __construct(Memecached $storage, int $lifetime, int $burndown)
0 ignored issues
show
Bug introduced by
The type Ignis\Mapper\Memecached was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
    {
17
        $this->storage = $storage;
18
        $this->lifetime = $lifetime;
19
        $this->burndown = $burndown;
20
    }
21
22
    public function store(Entity\Identity $identity)
23
    {
24
        $data = [
25
            'payload' => $identity->getPayload(),
26
            'account' => $identity->getAccountId(),
27
            'token' => $identity->getToken(),
28
            'createdOn' => null,
29
            'activatedOn' => null,
30
        ];
31
32
        if ($identity->getCreationTime()) {
33
            $data['createdOn'] = $identity->getCreationTime()->getTimestamp();
34
            $expires = $identity->getCreationTime()->getTimestamp() + $this->lifetime;
35
        }
36
37
        if ($identity->getActivationTime()) {
38
            $data['activatedOn'] = $identity->getActivationTime()->getTimestamp();
39
            $expires = $identity->getActivationTime()->getTimestamp() + $this->burndown;
40
        }
41
42
        $this->storage->set($identity->getKey(), $data, $expires);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $expires does not seem to be defined for all execution paths leading up to this point.
Loading history...
43
    }
44
45
    public function fetch(Entity\Identity $identity)
46
    {
47
        $data = $this->storage->get($identity->getKey());
48
49
        if (false === $data) {
50
            throw new Exception\EntityNotFound;
51
        }
52
53
        $identity->setPayload($data['payload']);
54
        $identity->setAccountId($data['account']);
55
        $identity->setToken($data['token']);
56
        $identity->setCreationTime((new DateTimeImmutable)->setTimestamp($data['createdOn']));
0 ignored issues
show
Bug introduced by
The type Ignis\Mapper\DateTimeImmutable was not found. Did you mean DateTimeImmutable? If so, make sure to prefix the type with \.
Loading history...
57
        
58
        if ($data['activatedOn']) {
59
            $identity->setActivationTime((new DateTimeImmutable)->setTimestamp($data['activatedOn']));
60
        }
61
    }
62
}
63