|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Utility; |
|
4
|
|
|
|
|
5
|
|
|
use Ps2alerts\Api\Contract\RedisAwareInterface; |
|
6
|
|
|
use Ps2alerts\Api\Contract\RedisAwareTrait; |
|
7
|
|
|
|
|
8
|
|
|
class RedisUtility implements RedisAwareInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use RedisAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
protected $flag = 'NOT-USED'; |
|
13
|
|
|
protected $misses = 0; |
|
14
|
|
|
protected $hits = 0; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Sets the Redis Missed Flag |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $flag |
|
20
|
|
|
*/ |
|
21
|
|
|
public function setFlag($flag) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->flag = $flag; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Sets the Redis Missed Flag, which the response class will pull out to send to the headers |
|
28
|
|
|
* |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getFlag() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->flag; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Sets the Redis Missed Flag, which the response class will pull out to send to the headers |
|
38
|
|
|
* |
|
39
|
|
|
* @return int |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getMissCount() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->misses; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Sets the Redis Missed Flag, which the response class will pull out to send to the headers |
|
48
|
|
|
* |
|
49
|
|
|
* @return int |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getHitCount() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->hits; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Checks redis for a entry and returns it decoded if exists |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $store Redis store to pull data from |
|
60
|
|
|
* @param string $type player|outfit |
|
61
|
|
|
* @param string $id ID of player or outfit |
|
62
|
|
|
* @param string $encodeType Flag to change format of data |
|
63
|
|
|
* |
|
64
|
|
|
* @return string|boolean |
|
65
|
|
|
*/ |
|
66
|
|
|
public function checkRedis($store = 'api', $type, $id, $encodeType = 'array') |
|
67
|
|
|
{ |
|
68
|
|
|
$redis = $this->getRedisDriver(); |
|
69
|
|
|
|
|
70
|
|
|
$key = "ps2alerts:{$store}:{$type}:{$id}"; |
|
71
|
|
|
|
|
72
|
|
|
if ($redis->exists($key)) { |
|
73
|
|
|
if ($encodeType === 'object') { |
|
74
|
|
|
$data = json_decode($redis->get($key)); |
|
75
|
|
|
} else if ($encodeType === 'array') { |
|
76
|
|
|
$data = json_decode($redis->get($key), true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->checkFlagScenario('hit'); |
|
80
|
|
|
$this->hits++; |
|
81
|
|
|
|
|
82
|
|
|
return $data; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->checkFlagScenario('miss'); |
|
86
|
|
|
$this->misses++; |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Stores the complete information in Redis |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $namespace Split between cache and API |
|
95
|
|
|
* @param string $type |
|
96
|
|
|
* @param string $id |
|
97
|
|
|
* @param string $data |
|
98
|
|
|
* @param integer $time Time in seconds to store data |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
* |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
*/ |
|
104
|
|
|
public function storeInRedis($namespace = 'api', $type, $id, $data, $time = 0) |
|
105
|
|
|
{ |
|
106
|
|
|
$redis = $this->getRedisDriver(); |
|
107
|
|
|
$key = "ps2alerts:{$namespace}:{$type}:{$id}"; |
|
108
|
|
|
|
|
109
|
|
|
$data = json_encode($data); |
|
110
|
|
|
|
|
111
|
|
|
// Check for errors #BRINGJSONEXCEPTIONS! |
|
112
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
113
|
|
|
throw new \Exception(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (!$time) { |
|
117
|
|
|
$time = 3600 * 24; // 1 day |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->checkFlagScenario('store'); |
|
121
|
|
|
|
|
122
|
|
|
return $redis->setEx($key, $time, $data); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Sets the missed flag for Redis based on scenario. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $mode |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
public function checkFlagScenario(string $mode) |
|
134
|
|
|
{ |
|
135
|
|
|
/* |
|
136
|
|
|
If all keys are missing: Miss |
|
137
|
|
|
If one key is found: Partial-Hit |
|
138
|
|
|
If ALL keys are found: Hit |
|
139
|
|
|
*/ |
|
140
|
|
|
|
|
141
|
|
|
if ($this->getFlag() === 'NOT-USED' || $this->getFlag() !== 'MISS - STORED') { |
|
142
|
|
|
if ($mode === 'miss') { |
|
143
|
|
|
$this->setFlag('MISS'); |
|
144
|
|
|
} |
|
145
|
|
|
if ($mode === 'store') { |
|
146
|
|
|
$this->setFlag('MISS - STORED'); |
|
147
|
|
|
} |
|
148
|
|
|
if ($mode === 'hit') { |
|
149
|
|
|
$this->setFlag('HIT'); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// If we've had a miss and we've previously marked as a hit, downgrade to Partial Hit. |
|
154
|
|
|
if ($mode === 'miss' && $this->getFlag() === 'HIT') { |
|
155
|
|
|
$this->setFlag('PARTIAL-HIT'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// Already set to Miss |
|
159
|
|
|
} |
|
160
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: