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 ( d32462...af31de )
by Hilari
05:14 queued 02:44
created

ChainCache::pushCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Cmp\Cache\Backend;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\NotFoundException;
7
use Cmp\Cache\TimeToLiveAwareCache;
8
use Cmp\Cache\Traits\MultiCacheTrait;
9
10
/**
11
 * Class ArrayCache
12
 * 
13
 * A simple backend powered by an array in memory
14
 *
15
 * @package Cmp\Cache\Infrastureture\Backend
16
 */
17
class ChainCache implements Cache
18
{
19
    use MultiCacheTrait;
20
21
    /**
22
     * Stored items
23
     * 
24
     * @var Cache[]
25
     */
26
    private $cache = [];
27
28
    /**
29
     * Pushes a cache in the chain
30
     *
31
     * @param Cache $cache
32
     *
33
     * @return $this
34
     */
35 1
    public function pushCache(Cache $cache)
36
    {
37 1
        $this->cache[] = $cache;
38
39 1
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function set($key, $item, $timeToLive = null)
46
    {
47 1
        $success = true;
48 1
        foreach ($this->cache as $cache) {
49 1
            $success = $success && $cache->set($key, $item, $timeToLive);
50
        }
51
52 1
        return $success;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function has($key)
59
    {
60 1
        foreach ($this->cache as $cache) {
61 1
            if ($cache->has($key)) {
62 1
                return true;
63
            }
64
        }
65
66 1
        return false;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function get($key, $default = null)
73
    {
74 1
        foreach ($this->cache as $index => $cache) {
75 1
            $item = $cache->get($key);
76 1
            if ($item) {
77 1
                $this->populatePreviousCaches($index, $key, $item, $cache->getTimeToLive($key));
78
79 1
                return $item;
80
            }
81
        }
82
83 1
        return $default;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function demand($key)
90
    {
91 1
        $item = $this->get($key);
92 1
        if (!$item) {
93 1
            throw new NotFoundException($key);
94
        }
95
96 1
        return $item;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 1
    public function delete($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104 1
        $success = true;
105 1
        foreach ($this->cache as $cache) {
106 1
            $success = $success && $cache->delete($key);
107
        }
108
109 1
        return $success;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function flush()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117 1
        $success = true;
118 1
        foreach ($this->cache as $cache) {
119 1
            $success = $success && $cache->flush();
120
        }
121
122 1
        return $success;
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
        foreach ($this->cache as $cache) {
135 1
            $timeToLive = $cache->getTimeToLive($key);
136 1
            if ($timeToLive) {
137 1
                return $timeToLive;
138
            }
139
        }
140
141 1
        return null;
142
    }
143
144
    /**
145
     * @param string   $index
146
     * @param string   $key
147
     * @param string   $item
148
     * @param int|null $timeToLive
149
     */
150 1
    private function populatePreviousCaches($index, $key, $item, $timeToLive)
151
    {
152 1
        for (--$index; $index >= 0 ; $index--) {
153 1
            $this->cache[$index]->set($key, $item, $timeToLive);
154
        }
155 1
    }
156
}
157