VariableCache   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 30
c 1
b 0
f 0
dl 0
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cacheInfo() 0 8 2
A store() 0 4 1
A delete() 0 6 2
A __construct() 0 7 6
A fetch() 0 12 3
1
<?php
2
3
namespace CodeBlog\RateLimit;
4
5
/**
6
 * Class CodeBlog VariableCache
7
 *
8
 * @author Whallysson Avelino <https://github.com/whallysson>
9
 * @package CodeBlog\RateLimit
10
 */
11
class VariableCache {
12
13
    /**
14
     * Directory where the data will be saved
15
     * @access private
16
     * @var string
17
     */
18
    private $cacheFolder;
19
20
    /**
21
     * Extent that will save the file
22
     * @access private
23
     * @var type string
0 ignored issues
show
Bug introduced by
The type CodeBlog\RateLimit\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     */
25
    private $ext = "cache";
26
27
    /**
28
     * Variable Array
29
     * @access private
30
     * @var type array
31
     */
32
    private $variables = [];
33
34
    /**
35
     * VariableCache constructor.
36
     * @param string|null $cacheFolder
37
     * @throws \Exception
38
     */
39
    function __construct(string $cacheFolder = null) {
40
        $folder = (substr($cacheFolder, "-1") == "/" ? $cacheFolder : "{$cacheFolder}/");
41
        $this->cacheFolder = $cacheFolder ? $folder : $_SERVER['DOCUMENT_ROOT'] . '/cache-variable/';
42
43
        if (!file_exists($this->cacheFolder) && !is_dir($this->cacheFolder)):
44
            if (!mkdir($this->cacheFolder, 0755)) {
45
                throw new \Exception("Could not create cache folder");
46
            }
47
        endif;
48
    }
49
50
    /**
51
     * Saves a variable in the cache
52
     * 
53
     * @param string $key
54
     * @param array $value
55
     * @param string $expire
56
     */
57
    public function store(string $key, array $value, $expire = 0) {
58
        $this->variables = array_merge($this->variables, ['expire' => time() + $expire]);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->varia...' => time() + $expire)) of type array is incompatible with the declared type CodeBlog\RateLimit\type of property $variables.

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...
Bug introduced by
$this->variables of type CodeBlog\RateLimit\type is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $this->variables = array_merge(/** @scrutinizer ignore-type */ $this->variables, ['expire' => time() + $expire]);
Loading history...
59
        $this->variables = array_merge($this->variables, ['data' => $value]);
60
        file_put_contents($this->cacheFolder . "{$key}.{$this->ext}", json_encode($this->variables));
61
    }
62
63
    /**
64
     * Returns the saved cache variables
65
     * 
66
     * @param string $key
67
     * @return boolean
68
     */
69
    public function fetch(string $key) {
70
        $archive = $this->cacheFolder . "{$key}.{$this->ext}";
71
        if (file_exists($archive)) {
72
            $content = json_decode(file_get_contents($archive), true);
73
74
            if ($content['expire'] > time()):
75
                return $content['data'];
76
            endif;
77
            unlink($archive);
78
        }
79
80
        return false;
81
    }
82
83
    /**
84
     * Delete the last cache on $key
85
     * 
86
     * @param type $key
87
     * @return boolean
88
     */
89
    public function delete(string $key) {
90
        $archive = $this->cacheFolder . "{$key}.{$this->ext}";
91
        if (file_exists($archive)) {
92
            unlink($archive);
93
        }
94
        return true;
95
    }
96
97
    /**
98
     * Returns all saved cache variables
99
     * 
100
     * @param string $key
101
     * @return boolean
102
     */
103
    public function cacheInfo(string $key) {
104
        $archive = $this->cacheFolder . "{$key}.{$this->ext}";
105
        if (file_exists($archive)) {
106
            $content = json_decode(file_get_contents($archive), true);
107
            return $content;
108
        }
109
110
        return false;
111
    }
112
113
}
114