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
Pull Request — master (#158)
by Bernardo Vieira da
12:24
created

GearmanCacheWrapper::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Gearman Bundle for Symfony2
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * Feel free to edit as you please, and have fun.
10
 *
11
 * @author Marc Morera <[email protected]>
12
 */
13
14
namespace Mmoreram\GearmanBundle\Service;
15
16
use Doctrine\Common\Cache\Cache;
17
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
18
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
19
20
/**
21
 * Gearman cache loader class
22
 *
23
 * This class has responsability of loading all gearman data structure
24
 * and cache it if needed.
25
 *
26
 * Also provides this data to external services
27
 *
28
 * @since 2.3.1
29
 */
30
class GearmanCacheWrapper implements CacheClearerInterface, CacheWarmerInterface
31
{
32
    /**
33
     * @var GearmanParser
34
     *
35
     * Gearman file parser
36
     */
37
    private $gearmanParser;
38
39
    /**
40
     * @var Cache
41
     *
42
     * Cache instance
43
     */
44
    private $cache;
45
46
    /**
47
     * @var string
48
     *
49
     * Cache id
50
     */
51
    private $cacheId;
52
53
    /**
54
     * @var array
55
     *
56
     * WorkerCollection with all workers and jobs available
57
     */
58
    private $workerCollection;
59
60
    /**
61
     * Construct method
62
     *
63
     * @param GearmanParser $gearmanParser Gearman Parser
64
     * @param Cache         $cache         Cache instance
65
     * @param string        $cacheId       Cache id
66
     */
67 4
    public function __construct(
68
        GearmanParser $gearmanParser,
69
        Cache $cache,
70
        $cacheId
71
    )
72
    {
73 4
        $this->gearmanParser = $gearmanParser;
74 4
        $this->cache = $cache;
75 4
        $this->cacheId = $cacheId;
76 4
    }
77
78
    /**
79
     * Return gearman file parser
80
     *
81
     * @return GearmanParser
82
     */
83 1
    public function getGearmanParser()
84
    {
85 1
        return $this->gearmanParser;
86
    }
87
88
    /**
89
     * Return cache
90
     *
91
     * @return Cache Cache
92
     */
93
    public function getCache()
94
    {
95
        return $this->cache;
96
    }
97
98
    /**
99
     * Return cache id
100
     *
101
     * @return string Cache id
102
     */
103
    public function getCacheId()
104
    {
105
        return $this->cacheId;
106
    }
107
108
    /**
109
     * Return workerCollection
110
     *
111
     * @return array all available workers
112
     */
113 2
    public function getWorkers()
114
    {
115 2
        return $this->workerCollection;
116
    }
117
118
    /**
119
     * loads Gearman cache, only if is not loaded yet
120
     *
121
     * @param Cache  $cache   Cache instance
122
     * @param string $cacheId Cache id
123
     *
124
     * @return GearmanCacheWrapper self Object
125
     */
126 4
    public function load(Cache $cache, $cacheId)
127
    {
128 4
        if ($cache->contains($cacheId)) {
129
130
            /**
131
             * Cache contains gearman structure
132
             */
133 3
            $this->workerCollection = $cache->fetch($cacheId);
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache->fetch($cacheId) of type * is incompatible with the declared type array of property $workerCollection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
134
135
        } else {
136
137
            /**
138
             * Cache is empty.
139
             *
140
             * Full structure must be generated and cached
141
             */
142 1
            $this->workerCollection = $this
143 1
                ->getGearmanParser()
144 1
                ->load()
145 1
                ->toArray();
146
147 1
            $cache->save($cacheId, $this->workerCollection);
148
        }
149
150 4
        return $this;
151
    }
152
153
    /**
154
     * flush all cache
155
     *
156
     * @param Cache  $cache   Cache instance
157
     * @param string $cacheId Cache id
158
     *
159
     * @return GearmanCacheWrapper self Object
160
     */
161
    public function flush(Cache $cache, $cacheId)
162
    {
163
        $cache->delete($cacheId);
164
165
        return $this;
166
    }
167
168
    /**
169
     * Cache clear implementation
170
     *
171
     * @param string $cacheDir The cache directory
172
     *
173
     * @return GearmanCacheWrapper self Object
174
     */
175
    public function clear($cacheDir)
176
    {
177
        $this->flush($this->getCache(), $this->getCacheId());
178
179
        return $this;
180
    }
181
182
    /**
183
     * Warms up the cache.
184
     *
185
     * @param string $cacheDir The cache directory
186
     *
187
     * @return GearmanCacheWrapper self Object
188
     */
189
    public function warmUp($cacheDir)
190
    {
191
        $this->load($this->getCache(), $this->getCacheId());
192
193
        return $this;
194
    }
195
196
    /**
197
     * Checks whether this warmer is optional or not.
198
     *
199
     * Optional warmers can be ignored on certain conditions.
200
     *
201
     * A warmer should return true if the cache can be
202
     * generated incrementally and on-demand.
203
     *
204
     * As GearmanBundle loads cache incrementaly so is optional
205
     *
206
     * @return Boolean true if the warmer is optional, false otherwise
207
     */
208 1
    public function isOptional()
209
    {
210 1
        return true;
211
    }
212
}
213