1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Check\Redis\WriteReadDelete; |
4
|
|
|
|
5
|
|
|
use Predis\Client as PredisClient; |
6
|
|
|
use Predis\PredisException; |
7
|
|
|
use TonicHealthCheck\Check\Redis\AbstractRedisCheck; |
8
|
|
|
use TonicHealthCheck\Check\Redis\WriteReadDelete\Exception\RedisDeleteCheckException; |
9
|
|
|
use TonicHealthCheck\Check\Redis\WriteReadDelete\Exception\RedisWRDCheckException; |
10
|
|
|
use TonicHealthCheck\Check\Redis\WriteReadDelete\Exception\RedisWriteReadCheckException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RedisWriteReadDeleteCheck. |
14
|
|
|
*/ |
15
|
|
|
class RedisWriteReadDeleteCheck extends AbstractRedisCheck |
16
|
|
|
{ |
17
|
|
|
const CHECK = 'redis-write-read-delete-check'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var PredisClient |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $checkNode |
26
|
|
|
* @param PredisClient $client |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct($checkNode, PredisClient $client) |
29
|
|
|
{ |
30
|
6 |
|
parent::__construct($checkNode); |
31
|
6 |
|
$this->setClient($client); |
32
|
6 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test redis write&read&delete. |
36
|
|
|
* |
37
|
|
|
* @throws RedisDeleteCheckException |
38
|
|
|
* @throws RedisWRDCheckException |
39
|
|
|
* @throws RedisWriteReadCheckException |
40
|
|
|
*/ |
41
|
6 |
|
public function performCheck() |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
6 |
|
$value1 = 'testData_123456'; |
45
|
6 |
|
$value2 = 'Other_98765'; |
46
|
6 |
|
$valueKey = 'test_health_check_val'; |
47
|
|
|
|
48
|
6 |
|
$valueOld = $this->getClient()->get($valueKey); |
49
|
6 |
|
$this->getClient()->set($valueKey, $value1); |
50
|
|
|
|
51
|
4 |
View Code Duplication |
if ($this->getClient()->get($valueKey) != $value1) { |
|
|
|
|
52
|
1 |
|
throw RedisWriteReadCheckException::doesNotSave($valueOld, $value1, $this->getClient()->get($valueKey)); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
$valueOld = $this->getClient()->get($valueKey); |
56
|
3 |
|
$this->getClient()->set($valueKey, $value2); |
57
|
|
|
|
58
|
3 |
View Code Duplication |
if ($this->getClient()->get($valueKey) != $value2) { |
|
|
|
|
59
|
1 |
|
throw RedisWriteReadCheckException::doesNotSave($valueOld, $value2, $this->getClient()->get($valueKey)); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$valueOld = $this->getClient()->get($valueKey); |
63
|
2 |
|
$this->getClient()->del($valueKey); |
64
|
|
|
|
65
|
2 |
|
if ($this->getClient()->exists($valueKey)) { |
66
|
2 |
|
throw RedisDeleteCheckException::doesNotDelete($valueOld, $this->getClient()->get($valueKey)); |
67
|
|
|
} |
68
|
5 |
|
} catch (PredisException $e) { |
69
|
1 |
|
throw RedisWRDCheckException::internalProblem($e); |
70
|
|
|
} |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return PredisClient |
75
|
|
|
*/ |
76
|
6 |
|
public function getClient() |
77
|
|
|
{ |
78
|
6 |
|
return $this->client; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param PredisClient $client |
83
|
|
|
*/ |
84
|
6 |
|
protected function setClient(PredisClient $client) |
85
|
|
|
{ |
86
|
6 |
|
$this->client = $client; |
87
|
6 |
|
} |
88
|
|
|
} |
89
|
|
|
|
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.