GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch v2 (a88462)
by Hilari
02:38
created

ArrayCache   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 127
rs 10
ccs 37
cts 40
cp 0.925

8 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 3
A has() 0 14 3
A get() 0 8 2
A demand() 0 13 3
A delete() 0 8 2
A flush() 0 6 1
A hasExpired() 0 4 2
A getTimeToLive() 0 14 3
1
<?php
2
3
namespace Cmp\Cache\Backend;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\CacheException;
7
use Cmp\Cache\Exceptions\ExpiredException;
8
use Cmp\Cache\Exceptions\NotFoundException;
9
use Cmp\Cache\FlushableCache;
10
use Cmp\Cache\PurgableCache;
11
use Cmp\Cache\Traits\MultiCacheTrait;
12
13
/**
14
 * Class ArrayCache
15
 * 
16
 * A simple backend powered by an array in memory
17
 *
18
 * @package Cmp\Cache\Infrastureture\Backend
19
 */
20
class ArrayCache implements Cache
21
{
22
    use MultiCacheTrait;
23
24
    /**
25
     * Stored items
26
     * 
27
     * @var array
28
     */
29
    private $items = [];
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function set($key, $value, $timeToLive = null)
35
    {
36 1
        $this->items[$key] = [
37 1
            'value'      => $value,
38 1
            'expireTime' => $timeToLive === null || $timeToLive === 0 ? null : time() + $timeToLive,
39
        ];
40
41 1
        return true;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function has($key)
48
    {
49 1
        if (!array_key_exists($key, $this->items)) {
50 1
            return false;
51
        }
52
53 1
        if ($this->hasExpired($key)) {
54 1
            $this->delete($key);
55
56 1
            return false;
57
        }
58
59 1
        return true;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function get($key, $default = null)
66
    {
67
        try {
68 1
            return $this->demand($key);
69 1
        } catch (CacheException $exception) {
70 1
            return $default;
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1
    public function demand($key)
78
    {
79 1
        if (!array_key_exists($key, $this->items)) {
80 1
            throw new NotFoundException($key);
81
        }
82
83 1
        if ($this->hasExpired($key)) {
84 1
            $this->delete($key);
85 1
            throw new ExpiredException($key);
86
        }
87
88 1
        return $this->items[$key]['value'];
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function delete($key)
95
    {
96 1
        if (array_key_exists($key, $this->items)) {
97 1
            unset($this->items[$key]);
98 1
        }
99
100 1
        return true;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function flush()
107
    {
108 1
        $this->items = [];
109
110 1
        return true;
111
    }
112
113
    /**
114
     * Checks whether an item as expired
115
     *
116
     * @param string $key
117
     *
118
     * @return bool
119
     */
120 1
    private function hasExpired($key)
121
    {
122 1
        return $this->items[$key]['expireTime'] !== null && $this->items[$key]['expireTime'] < time();
123
    }
124
125
    /**
126
     * Gets the remaining time to live for an item
127
     *
128
     * @param $key
129
     *
130
     * @return int|null
131
     */
132 1
    public function getTimeToLive($key)
133
    {
134 1
        if (!$this->has($key)) {
135
            return null;
136
        }
137
138 1
        if ($this->hasExpired($key)) {
139
            $this->delete($key);
140
141
            return null;
142
        }
143
144 1
        return $this->items[$key]['expireTime'] - time();
145
    }
146
}
147