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 ( 5b2683...2f55d1 )
by Miles
22:07
created

CacheProvider::getProvide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the m1\vars library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/vars
12
 * @version     0.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/vars/blob/master/LICENSE
16
 * @link        http://github.com/m1/vars/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\Vars\Cache;
20
21
use M1\Vars\Vars;
22
23
/**
24
 * The CacheProvider provides the cached file if one exists and is requested
25
 *
26
 * @since 0.1.0
27
 */
28
class CacheProvider
29
{
30
    /**
31
     * Has the cache been attempted
32
     *
33
     * @var bool $attempted
34
     */
35
    private $attempted = false;
36
37
    /**
38
     * How long for the cache to be fresh
39
     *
40
     * @var int $expire
41
     */
42
    private $expire;
43
44
    /**
45
     * Has the cache been found
46
     *
47
     * @var bool $hit
48
     */
49
    private $hit = false;
50
51
    /**
52
     * The loaded Vars object from cache
53
     *
54
     * @var \M1\Vars\Vars $loaded_vars
55
     */
56
    private $loaded_vars;
57
58
    /**
59
     * The cache file name
60
     *
61
     * @var string $name
62
     */
63
    private $name;
64
65
    /**
66
     * The specific path for the cache folder
67
     *
68
     * @var string $path
69
     */
70
    private $path;
71
72
    /**
73
     * Is the cache turned on
74
     *
75
     * @var boolean $provide
76
     */
77
    private $provide;
78
79
    /**
80
     * If cached, the time when the class was cached
81
     *
82
     * @var int $time
83
     */
84
    private $time;
85
86
    /**
87
     * Creates a new instance of the cacheProvider for Vars
88
     *
89
     * @param string|array $resource The main configuration resource
90
     * @param array        $options  The options being used for Vars
91
     */
92
    public function __construct($resource, $options)
93
    {
94
        $this->setProvide($options['cache']);
95
        $this->setPath($options['cache_path']);
96
97
        $this->expire = $options['cache_expire'];
98
        $this->name = md5(serialize($resource));
99
    }
100
101
    /**
102
     * Checks the cache to see if there is a valid cache available
103
     *
104
     * @return bool Returns true if has the cached resource
105
     */
106
    public function checkCache()
107
    {
108
        if ($this->provide && $this->path && !$this->getAttempted()) {
109
            $file = sprintf('%s/%s.php', $this->path, $this->name);
110
            $this->attempted = true;
111
112
            if (is_file($file) &&
113
                filemtime($file) >= (time() - $this->expire)) {
114
                $this->hit = true;
115
                return true;
116
            }
117
        }
118
        return false;
119
    }
120
121
    /**
122
     * Load the cached file into $this->loaded_vars
123
     */
124
    public function load()
125
    {
126
        $cached_file = sprintf('%s/%s.php', $this->path, $this->name);
127
        $this->loaded_vars = unserialize(file_get_contents($cached_file));
128
    }
129
130
    /**
131
     * Transfer the contents of the parent Vars object into a file for cache
132
     *
133
     * @param \M1\Vars\Vars $provide Does the cache want to be on or off
0 ignored issues
show
Bug introduced by
There is no parameter named $provide. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
134
     */
135
    public function makeCache(Vars $vars)
136
    {
137
        if ($this->provide) {
138
            $cache_file = sprintf('%s/%s.php', $this->path, $this->name);
139
            file_put_contents($cache_file, serialize($vars));
140
        }
141
    }
142
143
    /**
144
     * Returns if cache is on or off
145
     *
146
     * @return bool Is cache on or off
147
     */
148
    public function getProvide()
149
    {
150
        return $this->provide;
151
    }
152
153
    /**
154
     * Set the cache on or off
155
     *
156
     * @param bool $provide Does the cache want to be on or off
157
     *
158
     * @return \M1\Vars\Cache\CacheProvider
159
     */
160
    public function setProvide($provide)
161
    {
162
        $this->provide = $provide;
163
        return $this;
164
    }
165
166
    /**
167
     * Returns if the cache has been attempted
168
     *
169
     * @return bool Has the cache been attempted
170
     */
171
    public function getAttempted()
172
    {
173
        return $this->attempted;
174
    }
175
176
    /**
177
     * Returns the cache path
178
     *
179
     * @return string The cache path
180
     */
181
    public function getPath()
182
    {
183
        return $this->path;
184
    }
185
186
    /**
187
     * Sets the cache path
188
     *
189
     * @param string $cache_path The cache path to set
0 ignored issues
show
Bug introduced by
There is no parameter named $cache_path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
190
     *
191
     * @throws \InvalidArgumentException If the cache path does not exist or is not writable
192
     *
193
     * @return \M1\Vars\Cache\CacheProvider
194
     */
195 View Code Duplication
    public function setPath($path)
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...
196
    {
197
        if (is_null($path)) {
198
            return;
199
        }
200
201
        if (!is_dir($path) || !is_writable($path)) {
202
            throw new \InvalidArgumentException(sprintf(
203
                "'%s' cache path does not exist or is not writable",
204
                $path
205
            ));
206
        }
207
208
        $this->path = realpath($path);
209
        return $this;
210
    }
211
212
    /**
213
     * Returns how long the cache lasts for
214
     *
215
     * @return int Cache expire time
216
     */
217
    public function getExpire()
218
    {
219
        return $this->expire;
220
    }
221
222
    /**
223
     * Returns how long the cache lasts for
224
     *
225
     * @return int Cache expire time
226
     */
227
    public function isHit()
228
    {
229
        return $this->hit;
230
    }
231
232
    /**
233
     * Returns when the cache was made
234
     *
235
     * @return int Cache creation time
236
     */
237
    public function getTime()
238
    {
239
        return $this->time;
240
    }
241
242
    /**
243
     * Sets the time when the Vars was cached
244
     *
245
     * @param int $time Time when vars cached was created
246
     *
247
     * @return \M1\Vars\Cache\CacheProvider The cacheProvider object
248
     */
249
    public function setTime($time)
250
    {
251
        $this->time = $time;
252
        return $this;
253
    }
254
255
    /**
256
     * Returns the loaded Vars object
257
     *
258
     * @return \M1\Vars\Vars The loaded Vars object
259
     */
260
    public function getLoadedVars()
261
    {
262
        return $this->loaded_vars;
263
    }
264
265
}