1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tomaj\Hermes\Restart; |
5
|
|
|
|
6
|
|
|
use DateTime; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RedisRestart provides redis implementation of Tomaj\Hermes\Restart\RestartInterface |
11
|
|
|
* |
12
|
|
|
* Set UNIX timestamp (as `string`) to key `$key` (default `hermes_restart`) to restart Hermes. |
13
|
|
|
*/ |
14
|
|
|
class RedisRestart implements RestartInterface |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $key; |
18
|
|
|
|
19
|
|
|
/** @var \Predis\Client|\Redis */ |
20
|
|
|
private $redis; |
21
|
|
|
|
22
|
|
|
public function __construct($redis, string $key = 'hermes_restart') |
23
|
|
|
{ |
24
|
|
|
if (!(($redis instanceof \Predis\Client) || ($redis instanceof \Redis))) { |
|
|
|
|
25
|
|
|
throw new InvalidArgumentException('Predis\Client or Redis instance required'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->key = $key; |
29
|
|
|
$this->redis = $redis; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
* |
35
|
|
|
* Returns true: |
36
|
|
|
* |
37
|
|
|
* - if restart timestamp is set, |
38
|
|
|
* - and timestamp is not in future, |
39
|
|
|
* - and hermes was started ($startTime) before timestamp |
40
|
|
|
*/ |
41
|
|
|
public function shouldRestart(DateTime $startTime): bool |
42
|
|
|
{ |
43
|
|
|
// load UNIX timestamp from redis |
44
|
|
|
$restartTime = $this->redis->get($this->key); |
45
|
|
|
if ($restartTime === null) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
$restartTime = (int) $restartTime; |
49
|
|
|
|
50
|
|
|
// do not restart if restart time is in future |
51
|
|
|
if ($restartTime > time()) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// do not restart if hermes started after restart time |
56
|
|
|
if ($restartTime < $startTime->getTimestamp()) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
* |
66
|
|
|
* Sets to Redis value `$restartTime` (or current DateTime) to `$key` defined in constructor. |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function restart(DateTime $restartTime = null): bool |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if ($restartTime === null) { |
71
|
|
|
$restartTime = new DateTime(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->redis->set($this->key, $restartTime->format('U')); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.