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
Push — master ( 2f12c3...e53ef5 )
by Hilari
02:46
created

ArrayCache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 95
rs 10

7 Methods

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