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 ( 70c46b...316b2b )
by Hilari
02:09
created

ArrayCache::hasExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
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 delete($key)
30
    {
31
        if (array_key_exists($key, $this->items)) {
32
            unset($this->items[$key]);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function set($key, $value, $timeToLive = 0)
40
    {
41
        $this->items[$key] = [
42
            'value'      => $value,
43
            'expireTime' => $timeToLive === 0 ? $timeToLive : time() + $timeToLive,
44
        ];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function has($key)
51
    {
52
        if (!array_key_exists($key, $this->items)) {
53
            return false;
54
        }
55
56
        if ($this->hasExpired($key)) {
57
            return (bool) $this->delete($key);
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function get($key)
67
    {
68
        if (!array_key_exists($key, $this->items)) {
69
            throw new NotFoundException($key);
70
        }
71
72
        if ($this->hasExpired($key)) {
73
            $this->delete($key);
74
            throw new ExpiredException($key);
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function pull($key, $default = null)
84
    {
85
        try {
86
            return $this->get($key);
87
        } catch (CacheException $exception) {
88
            return $default;
89
        }
90
    }
91
92
    /**
93
     * Checks whether an item as expired
94
     * 
95
     * @param string $key
96
     *
97
     * @return bool
98
     */
99
    private function hasExpired($key)
100
    {
101
        return $this->items[$key]['expireTime'] !== 0 && $this->items[$key]['expireTime'] < time();
102
    }
103
}
104