Completed
Push — master ( 703943...42da56 )
by Babak
02:00
created

SmartMeta   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 11.76%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 207
ccs 8
cts 68
cp 0.1176
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A initCache() 0 11 3
A putCacheMeta() 0 7 1
A getCacheMeta() 0 8 2
A deleteCacheMeta() 0 7 1
A deleteCacheMetas() 0 7 1
A addCacheMeta() 0 6 1
A pushCacheMeta() 0 7 1
A pullCacheMeta() 0 7 1
A setAllCacheMeta() 0 5 1
A getAllCacheMeta() 0 5 1
A getId() 0 4 1
A setId() 0 4 1
A getGlobalKey() 0 4 1
A setGlobalKey() 0 4 1
A getExpireTime() 0 4 1
A setExpireTime() 0 4 1
A getCacheMetas() 0 4 1
A setCacheMetas() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alive
5
 * Date: 5/7/18
6
 * Time: 8:10 AM
7
 */
8
9
namespace Alive2212\LaravelSmartMeta;
10
11
use Illuminate\Support\Facades\Cache;
12
13
trait SmartMeta
14
{
15
    protected $id;
16
17
    protected $cacheMetas;
18
19
    protected $globalKey;
20
21
    protected $expireTime = 5; // 5 minutes
22
23
    /**
24
     *
25
     *
26
     * @return $this
27
     */
28
    public function initCache()
29
    {
30
        if(!($this->id>0)){
31
            $this->id = $this->toArray()['id'];
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
        }
33
        if (is_null($this->globalKey)){
34
            $this->globalKey = get_class($this) . '_' . $this->id;
35
        }
36
        $this->getAllCacheMeta();
37
        return $this;
38
    }
39
40
    /**
41
     *
42
     *
43
     * @param $key
44
     * @param $value
45
     * @return $this
46
     */
47
    public function putCacheMeta($key, $value)
48
    {
49
        $this->initCache();
50
        $value = [$key => $value];
51
        Cache::put($this->globalKey, $value, $this->expireTime);
52
        return $this;
53
    }
54
55
//    /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
//     * @return array
57
//     */
58
//    public function getAllCacheMeta(): array
59
//    {
60
//        return (Cache::get($this->globalKey));
61
//    }
62
63
64
    /**
65
     * @param $key
66
     * @param null $default
67
     * @param bool $getObj
68
     * @return null
69
     */
70
    public function getCacheMeta($key, $default = null, $getObj = false)
0 ignored issues
show
Unused Code introduced by
The parameter $getObj is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $this->initCache();
73
        if (isset($this->cacheMetas[$key])) {
74
            return $this->cacheMetas[$key];
75
        }
76
        return $default;
77
    }
78
79
    /**
80
     * @param $key
81
     * @return $this
82
     */
83
    public function deleteCacheMeta($key)
84
    {
85
        $this->initCache();
86
        unset($this->cacheMetas[$key]);
87
        $this->setAllCacheMeta();
88
        return $this;
89
    }
90
91
    /**
92
     *
93
     */
94
    public function deleteCacheMetas()
95
    {
96
        $this->initCache();
97
        $this->cacheMetas = null;
98
        $this->setAllCacheMeta();
99
        return $this;
100
    }
101
102
    /**
103
     * @param $key
104
     * @param $value
105
     */
106
    public function addCacheMeta($key, $value)
107
    {
108
        $this->initCache();
109
        $this->cacheMetas = array_add($this->cacheMetas, $key, $value);
110
        $this->setAllCacheMeta();
111
    }
112
113
    /**
114
     * @param $value
115
     * @return $this
116
     */
117
    public function pushCacheMeta($value)
118
    {
119
        $this->initCache();
120
        array_push($this->cacheMetas, $value);
121
        $this->setAllCacheMeta();
122
        return $this;
123
    }
124
125
    /**
126
     * @param $key
127
     * @param $default
128
     * @return $this
129
     */
130
    public function pullCacheMeta($key, $default = null)
131
    {
132
        $this->initCache();
133
        array_pull($this->cacheMetas, $key, $default);
134
        $this->setAllCacheMeta();
135
        return $this;
136
    }
137
138
    /**
139
     * @return $this
140
     */
141
    public function setAllCacheMeta()
142
    {
143
        Cache::put($this->globalKey, $this->cacheMetas, $this->expireTime);
144
        return $this;
145
    }
146
147
    /**
148
     * @return $this
149
     */
150
    public function getAllCacheMeta()
151
    {
152
        $this->cacheMetas = Cache::get($this->globalKey);
153
        return $this;
154
    }
155
156
    /**
157
     * @return mixed
158
     */
159 1
    public function getId()
160
    {
161 1
        return $this->id;
162
    }
163
164
    /**
165
     * @param mixed $id
166
     */
167 1
    public function setId($id)
168
    {
169 1
        $this->id = $id;
170 1
    }
171
172
    /**
173
     * @return mixed
174
     */
175
    public function getGlobalKey()
176
    {
177
        return $this->globalKey;
178
    }
179
180
    /**
181
     * @param mixed $globalKey
182
     */
183 1
    public function setGlobalKey($globalKey)
184
    {
185 1
        $this->globalKey = $globalKey;
186 1
    }
187
188
    /**
189
     * @return int
190
     */
191
    public function getExpireTime(): int
192
    {
193
        return $this->expireTime;
194
    }
195
196
    /**
197
     * @param int $expireTime
198
     */
199
    public function setExpireTime(int $expireTime)
200
    {
201
        $this->expireTime = $expireTime;
202
    }
203
204
    /**
205
     * @return mixed
206
     */
207
    public function getCacheMetas()
208
    {
209
        return $this->cacheMetas;
210
    }
211
212
    /**
213
     * @param mixed $cacheMetas
214
     */
215
    public function setCacheMetas($cacheMetas)
216
    {
217
        $this->cacheMetas = $cacheMetas;
218
    }
219
}