CacheKeys   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 35
dl 0
loc 118
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileKeys() 0 5 1
A loadKeys() 0 16 4
A putKey() 0 11 2
A __call() 0 8 1
A getKeys() 0 6 2
A storeKeys() 0 7 2
A __callStatic() 0 8 1
1
<?php
2
3
namespace Salah3id\Domains\Repository\Helpers;
4
5
/**
6
 * Class CacheKeys
7
 * @package Salah3id\Domains\Repository\Helpers
8
 * @author Anderson Andrade <[email protected]>
9
 */
10
class CacheKeys
11
{
12
13
    /**
14
     * @var string
15
     */
16
    protected static $storeFile = "repository-cache-keys.json";
17
18
    /**
19
     * @var array
20
     */
21
    protected static $keys = null;
22
23
    /**
24
     * @param $group
25
     * @param $key
26
     *
27
     * @return void
28
     */
29
    public static function putKey($group, $key)
30
    {
31
        self::loadKeys();
32
33
        self::$keys[$group] = self::getKeys($group);
34
35
        if (!in_array($key, self::$keys[$group])) {
36
            self::$keys[$group][] = $key;
37
        }
38
39
        self::storeKeys();
40
    }
41
42
    /**
43
     * @return array|mixed
44
     */
45
    public static function loadKeys()
46
    {
47
        if (!is_null(self::$keys) && is_array(self::$keys)) {
0 ignored issues
show
introduced by
The condition is_array(self::keys) is always true.
Loading history...
introduced by
The condition is_null(self::keys) is always false.
Loading history...
48
            return self::$keys;
49
        }
50
51
        $file = self::getFileKeys();
52
53
        if (!file_exists($file)) {
54
            self::storeKeys();
55
        }
56
57
        $content = file_get_contents($file);
58
        self::$keys = json_decode($content, true);
59
60
        return self::$keys;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public static function getFileKeys()
67
    {
68
        $file = storage_path("framework/cache/" . self::$storeFile);
69
70
        return $file;
71
    }
72
73
    /**
74
     * @return int
75
     */
76
    public static function storeKeys()
77
    {
78
        $file = self::getFileKeys();
79
        self::$keys = is_null(self::$keys) ? [] : self::$keys;
0 ignored issues
show
introduced by
The condition is_null(self::keys) is always false.
Loading history...
80
        $content = json_encode(self::$keys);
81
82
        return file_put_contents($file, $content);
83
    }
84
85
    /**
86
     * @param $group
87
     *
88
     * @return array|mixed
89
     */
90
    public static function getKeys($group)
91
    {
92
        self::loadKeys();
93
        self::$keys[$group] = isset(self::$keys[$group]) ? self::$keys[$group] : [];
94
95
        return self::$keys[$group];
96
    }
97
98
    /**
99
     * @param $method
100
     * @param $parameters
101
     *
102
     * @return mixed
103
     */
104
    public static function __callStatic($method, $parameters)
105
    {
106
        $instance = new static;
107
108
        return call_user_func_array([
109
            $instance,
110
            $method
111
        ], $parameters);
112
    }
113
114
    /**
115
     * @param $method
116
     * @param $parameters
117
     *
118
     * @return mixed
119
     */
120
    public function __call($method, $parameters)
121
    {
122
        $instance = new static;
123
124
        return call_user_func_array([
125
            $instance,
126
            $method
127
        ], $parameters);
128
    }
129
}
130