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.

CacheProvider::setProvide()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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     1.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\Traits\PathTrait;
22
use M1\Vars\Vars;
23
24
/**
25
 * The CacheProvider provides the cached file if one exists and is requested
26
 *
27
 * @since 0.1.0
28
 */
29
class CacheProvider
30
{
31
    /**
32
     * Used for path functions and variables
33
     */
34
    use PathTrait;
35
36
    /**
37
     * Has the cache been attempted
38
     *
39
     * @var bool $attempted
40
     */
41
    private $attempted = false;
42
43
    /**
44
     * How long for the cache to be fresh
45
     *
46
     * @var int $expire
47
     */
48
    private $expire;
49
50
    /**
51
     * Has the cache been found
52
     *
53
     * @var bool $hit
54
     */
55
    private $hit = false;
56
57
    /**
58
     * The loaded Vars object from cache
59
     *
60
     * @var \M1\Vars\Vars $loaded_vars
61
     */
62
    private $loaded_vars;
63
64
    /**
65
     * The cache file name
66
     *
67
     * @var string $name
68
     */
69
    private $name;
70
71
    /**
72
     * Is the cache turned on
73
     *
74
     * @var boolean $provide
75
     */
76
    private $provide;
77
78
    /**
79
     * If cached, the time when the class was cached
80
     *
81
     * @var int $time
82
     */
83
    private $time;
84
85
    /**
86
     * Creates a new instance of the cacheProvider for Vars
87
     *
88
     * @param string|array $resource The main configuration resource
89
     * @param array        $options  The options being used for Vars
90
     */
91 86
    public function __construct($resource, $options)
92
    {
93 86
        $this->setProvide($options['cache']);
94 86
        $this->setPath($options['cache_path'], true);
95
96 85
        $this->expire = $options['cache_expire'];
97 85
        $this->name = md5(serialize($resource));
98 85
    }
99
100
    /**
101
     * Checks the cache to see if there is a valid cache available
102
     *
103
     * @return bool Returns true if has the cached resource
104
     */
105 84
    public function checkCache()
106
    {
107 84
        if ($this->provide && $this->path && !$this->getAttempted()) {
108 7
            $file = sprintf('%s/vars/%s.php', $this->path, $this->name);
109 7
            $this->attempted = true;
110
111 7
            if (is_file($file) &&
112 7
                filemtime($file) >= (time() - $this->expire)) {
113 3
                $this->hit = true;
114 3
                return true;
115
            }
116
        }
117 84
        return false;
118
    }
119
120
    /**
121
     * Load the cached file into $this->loaded_vars
122
     */
123 3
    public function load()
124
    {
125 3
        $cached_file = sprintf('%s/vars/%s.php', $this->path, $this->name);
126 3
        $this->loaded_vars = unserialize(file_get_contents($cached_file));
127 3
    }
128
129
    /**
130
     * Transfer the contents of the parent Vars object into a file for cache
131
     *
132
     * @param \M1\Vars\Vars $vars Parent vars object
133
     *
134
     * @codeCoverageIgnore
135
     */
136
    public function makeCache(Vars $vars)
137
    {
138
        if ($this->provide) {
139
            $cache_folder = sprintf("%s/vars", $this->path);
140
            if (!file_exists($cache_folder)) {
141
                mkdir($cache_folder, 0777, true);
142
            }
143
144
            $cache_file = sprintf('%s/%s.php', $cache_folder, $this->name);
145
            file_put_contents($cache_file, serialize($vars));
146
        }
147
    }
148
149
    /**
150
     * Returns if cache is on or off
151
     *
152
     * @return bool Is cache on or off
153
     */
154 74
    public function getProvide()
155
    {
156 74
        return $this->provide;
157
    }
158
159
    /**
160
     * Set the cache on or off
161
     *
162
     * @param bool $provide Does the cache want to be on or off
163
     *
164
     * @return \M1\Vars\Cache\CacheProvider
165
     */
166 86
    public function setProvide($provide)
167
    {
168 86
        $this->provide = $provide;
169 86
        return $this;
170
    }
171
172
    /**
173
     * Returns if the cache has been attempted
174
     *
175
     * @return bool Has the cache been attempted
176
     */
177 7
    public function getAttempted()
178
    {
179 7
        return $this->attempted;
180
    }
181
182
    /**
183
     * Returns how long the cache lasts for
184
     *
185
     * @return int Cache expire time
186
     */
187 2
    public function getExpire()
188
    {
189 2
        return $this->expire;
190
    }
191
192
    /**
193
     * Returns how long the cache lasts for
194
     *
195
     * @return int Cache expire time
196
     */
197 66
    public function isHit()
198
    {
199 66
        return $this->hit;
200
    }
201
202
    /**
203
     * Returns when the cache was made
204
     *
205
     * @return int Cache creation time
206
     */
207 3
    public function getTime()
208
    {
209 3
        return $this->time;
210
    }
211
212
    /**
213
     * Sets the time when the Vars was cached
214
     *
215
     * @param int $time Time when vars cached was created
216
     *
217
     * @return \M1\Vars\Cache\CacheProvider The cacheProvider object
218
     */
219 66
    public function setTime($time)
220
    {
221 66
        $this->time = $time;
222 66
        return $this;
223
    }
224
225
    /**
226
     * Returns the loaded Vars object
227
     *
228
     * @return \M1\Vars\Vars The loaded Vars object
229
     */
230 3
    public function getLoadedVars()
231
    {
232 3
        return $this->loaded_vars;
233
    }
234
}
235