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.

ChainCache::populatePreviousCaches()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Cmp\Cache\Backend;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\NotFoundException;
7
use Cmp\Cache\Traits\MultiCacheTrait;
8
9
/**
10
 * Class ArrayCache
11
 * 
12
 * A simple backend powered by an array in memory
13
 *
14
 * @package Cmp\Cache\Infrastureture\Backend
15
 */
16
class ChainCache extends TaggableCache
17
{
18
    use MultiCacheTrait;
19
20
    /**
21
     * Stored items
22
     * 
23
     * @var Cache[]
24
     */
25
    private $caches = [];
26
27
    /**
28
     * Pushes a cache in the chain
29
     *
30
     * @param Cache $cache
31
     *
32
     * @return $this
33
     */
34 1
    public function pushCache(Cache $cache)
35
    {
36 1
        $this->caches[] = $cache;
37
38 1
        return $this;
39
    }
40
41
    /**
42
     * Returns the caches in the chain
43
     * 
44
     * @return \Cmp\Cache\Cache[]
45
     */
46 1
    public function getCaches()
47
    {
48 1
        return $this->caches;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function set($key, $item, $timeToLive = null)
55
    {
56 1
        $success = true;
57 1
        foreach ($this->caches as $cache) {
58 1
            $success = $success && $cache->set($key, $item, $timeToLive);
59 1
        }
60
61 1
        return $success;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function has($key)
68
    {
69 1
        foreach ($this->caches as $cache) {
70 1
            if ($cache->has($key)) {
71 1
                return true;
72
            }
73 1
        }
74
75 1
        return false;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 1
    public function get($key, $default = null)
82
    {
83 1
        foreach ($this->caches as $index => $cache) {
84 1
            $item = $cache->get($key);
85 1
            if ($item) {
86 1
                $this->populatePreviousCaches($index, $key, $item, $cache->getTimeToLive($key));
87
88 1
                return $item;
89
            }
90 1
        }
91
92 1
        return $default;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function demand($key)
99
    {
100 1
        $item = $this->get($key);
101 1
        if (!$item) {
102 1
            throw new NotFoundException($key);
103
        }
104
105 1
        return $item;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 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...
112
    {
113 1
        $success = true;
114 1
        foreach ($this->caches as $cache) {
115 1
            $success = $success && $cache->delete($key);
116 1
        }
117
118 1
        return $success;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 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...
125
    {
126 1
        $success = true;
127 1
        foreach ($this->caches as $cache) {
128 1
            $success = $success && $cache->flush();
129 1
        }
130
131 1
        return $success;
132
    }
133
134
    /**
135
     * Gets the remaining time to live for an item
136
     *
137
     * @param $key
138
     *
139
     * @return int|null
140
     */
141 1
    public function getTimeToLive($key)
142
    {
143 1
        foreach ($this->caches as $cache) {
144 1
            $timeToLive = $cache->getTimeToLive($key);
145 1
            if ($timeToLive) {
146 1
                return $timeToLive;
147
            }
148 1
        }
149
150 1
        return null;
151
    }
152
153
    /**
154
     * @param string   $index
155
     * @param string   $key
156
     * @param string   $item
157
     * @param int|null $timeToLive
158
     */
159 1
    private function populatePreviousCaches($index, $key, $item, $timeToLive)
160
    {
161 1
        for (--$index; $index >= 0; $index--) {
162 1
            $this->caches[$index]->set($key, $item, $timeToLive);
163 1
        }
164 1
    }
165
}
166