Completed
Push — master ( 00b680...38e9d3 )
by Tomas
25:27 queued 10:32
created

RedisShutdown   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A shouldShutdown() 0 21 4
A shutdown() 0 18 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Tomaj\Hermes\Shutdown;
5
6
use DateTime;
7
use InvalidArgumentException;
8
9
/**
10
 * Class RedisShutdown provides redis implementation of Tomaj\Hermes\Shutdown\ShutdownInterface
11
 *
12
 * Set UNIX timestamp (as `string`) to key `$key` (default `hermes_shutdown`) to shutdown Hermes workoer.
13
 */
14
class RedisShutdown implements ShutdownInterface
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_shutdown')
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 shutdown timestamp is set,
38
     * - and timestamp is not in future,
39
     * - and hermes was started ($startTime) before timestamp
40
     */
41
    public function shouldShutdown(DateTime $startTime): bool
42
    {
43
        // load UNIX timestamp from redis
44
        $shutdownTime = $this->redis->get($this->key);
45
        if ($shutdownTime === null) {
46
            return false;
47
        }
48
        $shutdownTime = (int) $shutdownTime;
49
50
        // do not shutdown if shutdown time is in future
51
        if ($shutdownTime > time()) {
52
            return false;
53
        }
54
55
        // do not shutdown if hermes started after shutdown time
56
        if ($shutdownTime < $startTime->getTimestamp()) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * Sets to Redis value `$shutdownTime` (or current DateTime) to `$key` defined in constructor.
67
     */
68
    public function shutdown(DateTime $shutdownTime = null): bool
69
    {
70
        if ($shutdownTime === null) {
71
            $shutdownTime = new DateTime();
72
        }
73
74
        $response = $this->redis->set($this->key, $shutdownTime->format('U'));
75
76
        if ($this->redis instanceof \Redis) {
77
            // \Redis::set() returns TRUE/FALSE
78
            return $response;
79
        }
80
81
        if ($this->redis instanceof \Predis\Client) {
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...
82
            /** @var \Predis\Response\Status $response */
83
            return $response->getPayload() === 'OK';
84
        }
85
    }
86
}
87