Completed
Push — master ( 5c8a13...04755b )
by Novikov
01:34
created

StorageManager::setProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NV\RequestLimitBundle\Storage;
4
5
6
use NV\RequestLimitBundle\Storage\Provider\ProviderInterface;
7
8
class StorageManager
9
{
10
    /**
11
     * @var ProviderInterface
12
     */
13
    private $provider;
14
15
    /**
16
     * @param $provider
17
     */
18
    public function setProvider(ProviderInterface $provider)
19
    {
20
        $this->provider = $provider;
21
    }
22
23
    /**
24
     * @param $key
25
     * @return bool
26
     */
27
    public function hasItem($key)
28
    {
29
        return ($this->provider->get($key) && $this->stillRestricted($key)) ? true : false;
30
    }
31
32
    /**
33
     * @param $key
34
     * @return mixed
35
     */
36
    public function getItem($key)
37
    {
38
        return $this->provider->get($key);
39
    }
40
41
    /**
42
     * @param $key
43
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
44
     */
45
    public function setItem($key, $value = null)
46
    {
47
        $value = $value ? : new \DateTime('+ 10 minutes');
48
        $this->provider->set($key, $value->getTimestamp());
49
    }
50
51
    /**
52
     * @param $key
53
     */
54
    public function removeItem($key)
55
    {
56
        $this->provider->remove($key);
57
    }
58
59
    /**
60
     * @return int
61
     */
62
    public function getItemsCount()
63
    {
64
        return $this->provider->getItemsCount();
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function fetchAllItems()
71
    {
72
        return $this->provider->fetchAllItems();
73
    }
74
75
    /**
76
     * @param $key
77
     * @return bool
78
     */
79
    private function stillRestricted($key)
80
    {
81
        $timestamp        = $this->provider->get($key);
82
        $currentDate      = new \DateTime();
83
        $currentTimestamp = $currentDate->getTimestamp();
84
85
        if ($timestamp < $currentTimestamp) {
86
            $this->removeItem($key);
87
            return false;
88
        }
89
90
        return true;
91
    }
92
}
93