Completed
Push — master ( 45aada...26ef87 )
by Matthew
02:32
created

AbstractLoader::getFromRedis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ps2alerts\Api\Loader;
4
5
use Ps2Alerts\Api\Contract\RedisAwareInterface;
6
use Ps2Alerts\Api\Contract\RedisAwareTrait;
7
8
abstract class AbstractLoader implements RedisAwareInterface
9
{
10
    use RedisAwareTrait;
11
12
    /**
13
    * Flag whether or not the result is allowed to be cached
14
    *
15
    * @var boolean
16
    */
17
18
    protected $cacheable = true;
19
    /**
20
     * Redis key namespace
21
     *
22
     * @var string
23
     */
24
    protected $cacheNamespace;
25
26
    /**
27
     * Type of statistics|metrics we're pulling. Used for building the Redis key
28
     * @var string
29
     */
30
    protected $type;
31
32
    /**
33
     * Cache timer
34
     *
35
     * @var integer
36
     */
37
    protected $cacheExpireTime = 10800; // 3 hours
38
39
    /**
40
     * Sets a flag whether or not the content should be cached
41
     *
42
     * @param boolean $toggle
43
     */
44
    public function setCacheable($toggle)
45
    {
46
        // If statement is here because can't figure out how to typehint a boolean...
47
        if ($toggle === true || $toggle === false) {
48
            $this->cacheable = $toggle;
49
        }
50
    }
51
52
    /**
53
     * Returns cacheable property
54
     *
55
     * @return boolean
56
     */
57
    public function getCacheable()
58
    {
59
        return $this->cacheable;
60
    }
61
62
    /**
63
     * Sets the Cache namespace key for Redis
64
     *
65
     * @param string $string
66
     */
67
    public function setCacheNamespace($string)
68
    {
69
        $this->cacheNamespace = $string;
70
    }
71
72
    /**
73
     * Gets the Cache namespace key for Redis
74
     *
75
     * @param string $string
0 ignored issues
show
Bug introduced by
There is no parameter named $string. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     */
77
    public function getCacheNamespace()
78
    {
79
        return $this->cacheNamespace;
80
    }
81
82
    /**
83
     * Sets the type of statistics|metrics we're looking for
84
     *
85
     * @param string $type
86
     */
87
    public function setType($type)
88
    {
89
        $this->type = $type;
90
    }
91
92
    /**
93
     * Returns the type of statistics|metrics
94
     *
95
     * @return string
96
     */
97
    public function getType()
98
    {
99
        return $this->type;
100
    }
101
102
    /**
103
     * Sets the cache expire time
104
     *
105
     * @param integer $secs
106
     */
107
    public function setCacheExpireTime($secs)
108
    {
109
        $this->cacheExpireTime = $secs;
110
    }
111
112
    /**
113
     * Gets the cache expire time
114
     *
115
     * @return integer
116
     */
117
    public function getCacheExpireTime()
118
    {
119
        return $this->cacheExpireTime;
120
    }
121
122
    /**
123
     * Checks for a key within Redis and returns it's existance
124
     *
125
     * @param  string  $key
126
     * @return boolean
127
     */
128
    public function checkRedis($key)
129
    {
130
        return $this->getRedisDriver()->exists($key);
131
    }
132
133
    /**
134
     * Retrieves a key from within Redis
135
     *
136
     * @param  string $key
137
     * @return string JSON decoded value
138
     */
139
    public function getFromRedis($key)
140
    {
141
        return json_decode($this->getRedisDriver()->get($key), JSON_BIGINT_AS_STRING);
142
    }
143
144
    /**
145
     * Sets JSON encoded value within Redis with an expiry in seconds
146
     *
147
     * @param string  $key
148
     * @param mixed   $value   Data to encode into JSON and store
149
     * @param integer $expires Cache expiry time in seconds
150
     */
151
    public function setExpireKey($key, $value, $expires)
152
    {
153
        return $this->getRedisDriver()->setEx($key, $expires, $value);
154
    }
155
156
    /**
157
     * Caches the data gathered and then returns again to the source script
158
     *
159
     * @param  mixed  $data Data to store
160
     * @param  string $key  Redis key to be used to store the data
161
     *
162
     * @return mixed  $data
163
     */
164
    public function cacheAndReturn($data, $key = null)
165
    {
166
        // Encode here so that the numbers can be converted to ints
167
        $data = json_encode($data, JSON_NUMERIC_CHECK);
168
169
        // Only cache if we're allowed to cache it
170
        if ($this->getCacheable() === true) {
171
            $this->setExpireKey($key, $data, $this->getCacheExpireTime());
172
        }
173
174
        // Decode again so it can be used again in the app.
175
        // This may seem like a performance overhead, but it ensures consistency.
176
        // The function is only going to be fired when cache is missing.
177
        return json_decode($data);
178
    }
179
}
180