Completed
Push — master ( 1b6642...22d6fd )
by Tomas
29s queued 10s
created

RedisRestart::shouldRestart()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
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))) {
0 ignored issues
show
Bug introduced by
The class Predis\Client does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
69
    {
70
        if ($restartTime === null) {
71
            $restartTime = new DateTime();
72
        }
73
74
        return $this->redis->set($this->key, $restartTime->format('U'));
75
    }
76
}
77