CacheKeys::storeKeys()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 8
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Database\Helpers;
12
13
/**
14
 * Class CacheKeys.
15
 */
16 View Code Duplication
class CacheKeys
0 ignored issues
show
Duplication introduced by
This class 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...
17
{
18
    /**
19
     * @var string
20
     */
21
    protected static $storeFile = 'repository-cache-keys.json';
22
23
    /**
24
     * @var array
25
     */
26
    protected static $keys = null;
27
28
    /**
29
     * @param $group
30
     * @param $key
31
     *
32
     * @return void
33
     */
34
    public static function putKey($group, $key)
35
    {
36
        self::loadKeys();
37
38
        self::$keys[$group] = self::getKeys($group);
39
40
        if (!in_array($key, self::$keys[$group])) {
41
            self::$keys[$group][] = $key;
42
        }
43
44
        self::storeKeys();
45
    }
46
47
    /**
48
     * @param $group
49
     *
50
     * @return array|mixed
51
     */
52
    public static function getKeys($group)
53
    {
54
        self::loadKeys();
55
        self::$keys[$group] = isset(self::$keys[$group]) ? self::$keys[$group] : [];
56
57
        return self::$keys[$group];
58
    }
59
60
    /**
61
     * @return array|mixed
62
     */
63
    public static function loadKeys()
64
    {
65
        if (!is_null(self::$keys) && is_array(self::$keys)) {
66
            return self::$keys;
67
        }
68
69
        $file = self::getFileKeys();
70
71
        if (!file_exists($file)) {
72
            self::storeKeys();
73
        }
74
75
        $content    = file_get_contents($file);
76
        self::$keys = json_decode($content, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($content, true) of type * is incompatible with the declared type array of property $keys.

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...
77
78
        return self::$keys;
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public static function storeKeys()
85
    {
86
        $file       = self::getFileKeys();
87
        self::$keys = is_null(self::$keys) ? [] : self::$keys;
88
        $content    = json_encode(self::$keys);
89
90
        return file_put_contents($file, $content);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public static function getFileKeys()
97
    {
98
        $file = storage_path('framework/cache/' . self::$storeFile);
99
100
        return $file;
101
    }
102
103
    /**
104
     * @param $method
105
     * @param $parameters
106
     *
107
     * @return mixed
108
     */
109
    public static function __callStatic($method, $parameters)
110
    {
111
        $instance = new static();
112
113
        return call_user_func_array([$instance, $method], $parameters);
114
    }
115
116
    /**
117
     * @param $method
118
     * @param $parameters
119
     *
120
     * @return mixed
121
     */
122
    public function __call($method, $parameters)
123
    {
124
        $instance = new static();
125
126
        return call_user_func_array([$instance, $method], $parameters);
127
    }
128
}
129