StorageManager::getItem()   A
last analyzed

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
use NV\RequestLimitBundle\Storage\Provider\ProviderInterface;
6
7
class StorageManager
8
{
9
    /**
10
     * @var ProviderInterface
11
     */
12
    public $provider;
13
14
    /**
15
     * @param $provider
16
     */
17
    public function setProvider(ProviderInterface $provider)
18
    {
19
        $this->provider = $provider;
20
    }
21
22
    /**
23
     * @param $key
24
     * @return bool
25
     */
26
    public function hasItem($key)
27
    {
28
        return ($this->provider->get($key) && $this->stillRestricted($key)) ? true : false;
29
    }
30
31
    /**
32
     * @param $key
33
     * @return mixed
34
     */
35
    public function getItem($key)
36
    {
37
        return $this->provider->get($key);
38
    }
39
40
    /**
41
     * @param $key
42
     * @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...
43
     */
44
    public function setItem($key, $value = null)
45
    {
46
        $value = $value ?: new \DateTime('+ 10 minutes');
47
        $this->provider->set($key, $value->getTimestamp());
48
    }
49
50
    /**
51
     * @param $key
52
     */
53
    public function removeItem($key)
54
    {
55
        $this->provider->remove($key);
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getItemsCount()
62
    {
63
        return $this->provider->getItemsCount();
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function fetchAllItems()
70
    {
71
        return $this->provider->fetchAllItems();
72
    }
73
74
    /**
75
     * @param $key
76
     * @return bool
77
     */
78
    private function stillRestricted($key)
79
    {
80
        $timestamp        = $this->provider->get($key);
81
        $currentDate      = new \DateTime();
82
        $currentTimestamp = $currentDate->getTimestamp();
83
84
        if ($timestamp < $currentTimestamp) {
85
            $this->removeItem($key);
86
            return false;
87
        }
88
89
        return true;
90
    }
91
}
92