Completed
Push — master ( 195654...bc8a63 )
by Tomas
39:26 queued 24:30
created

PredisShutdown::shouldShutdown()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
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\Shutdown;
5
6
use DateTime;
7
use Predis\Client;
8
9
/**
10
 * Class PredisShutdown provides predis implementation of Tomaj\Hermes\Shutdown\ShutdownInterface
11
 *
12
 * Set UNIX timestamp (as `string`) to key `$key` (default `hermes_shutdown`) to shutdown Hermes worker.
13
 */
14
class PredisShutdown implements ShutdownInterface
15
{
16
    /** @var string */
17
    private $key;
18
19
    /** @var Client */
20
    private $redis;
21
22
    public function __construct(Client $redis, string $key = 'hermes_shutdown')
23
    {
24
        $this->redis = $redis;
25
        $this->key = $key;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * Returns true:
32
     *
33
     * - if shutdown timestamp is set,
34
     * - and timestamp is not in future,
35
     * - and hermes was started ($startTime) before timestamp
36
     */
37 View Code Duplication
    public function shouldShutdown(DateTime $startTime): 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...
38
    {
39
        // load UNIX timestamp from redis
40
        $shutdownTime = $this->redis->get($this->key);
41
        if ($shutdownTime === null) {
42
            return false;
43
        }
44
        $shutdownTime = (int) $shutdownTime;
45
46
        // do not shutdown if shutdown time is in future
47
        if ($shutdownTime > time()) {
48
            return false;
49
        }
50
51
        // do not shutdown if hermes started after shutdown time
52
        if ($shutdownTime < $startTime->getTimestamp()) {
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * Sets to Redis value `$shutdownTime` (or current DateTime) to `$key` defined in constructor.
63
     */
64 View Code Duplication
    public function shutdown(DateTime $shutdownTime = 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...
65
    {
66
        if ($shutdownTime === null) {
67
            $shutdownTime = new DateTime();
68
        }
69
70
        /** @var \Predis\Response\Status $response */
71
        $response = $this->redis->set($this->key, $shutdownTime->format('U'));
72
73
        return $response->getPayload() === 'OK';
74
    }
75
}
76