Completed
Push — master ( 18ed91...48076b )
by Kevin
03:22
created

BaseRedisDriver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.19%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 117
ccs 33
cts 37
cp 0.8919
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
rPush() 0 1 ?
lPop() 0 1 ?
A push() 0 4 1
A pop() 0 18 3
A release() 0 4 1
A delete() 0 4 1
A encodePayload() 0 7 1
C decodePayload() 0 26 7
1
<?php
2
3
namespace Zenstruck\Queue\Driver;
4
5
use Zenstruck\Queue\Driver;
6
use Zenstruck\Queue\Job;
7
use Zenstruck\Queue\Payload;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
abstract class BaseRedisDriver implements Driver
13
{
14
    private $queueName;
15
16
    /**
17
     * @param string $queueName
18
     */
19 2
    public function __construct($queueName)
20
    {
21 2
        $this->queueName = $queueName;
22 2
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function push(Payload $payload)
28
    {
29 2
        $this->rPush($this->queueName, $this->encodePayload($payload));
30 2
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function pop()
36
    {
37 2
        $rawPayload = $this->lPop($this->queueName);
38
39 2
        if (null === $rawPayload) {
40
            // empty queue
41 2
            return null;
42
        }
43
44 2
        if (null === $job = $this->decodePayload($rawPayload)) {
45
            // can't handle, requeue
46 2
            $this->rPush($this->queueName, $rawPayload);
47
48 2
            return null;
49
        }
50
51 2
        return $job;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function release(Job $job)
58
    {
59 2
        $this->rPush($this->queueName, $this->encodePayload($job->payload(), $job->attempts()));
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function delete(Job $job)
66
    {
67
        // noop - job is deleted during pop
68 2
    }
69
70
    /**
71
     * @param Payload $payload
72
     * @param int     $attempts
73
     *
74
     * @return string
75
     */
76 2
    private function encodePayload(Payload $payload, $attempts = 0)
77
    {
78 2
        return json_encode([
79 2
            'payload' => $payload,
80 2
            'attempts' => $attempts,
81 2
        ]);
82
    }
83
84
    /**
85
     * @param string $rawPayload
86
     *
87
     * @return Job|null
88
     */
89 2
    private function decodePayload($rawPayload)
90
    {
91 2
        $decodedPayload = json_decode($rawPayload, true);
92
93 2
        if (JSON_ERROR_NONE !== json_last_error()) {
94 2
            return null;
95
        }
96
97 2
        if (!is_array($decodedPayload)) {
98
            return null;
99
        }
100
101 2
        if (!isset($decodedPayload['payload']) || !isset($decodedPayload['attempts'])) {
102
            return null;
103
        }
104
105 2
        if (!is_array($decodedPayload['payload'])) {
106
            return null;
107
        }
108
109 2
        if (null === $payload = Payload::fromArray($decodedPayload['payload'])) {
110
            return null;
111
        }
112
113 2
        return new Job($payload, $decodedPayload['attempts'] + 1);
114
    }
115
116
    /**
117
     * @param string $queueName
118
     * @param string $payload
119
     */
120
    abstract protected function rPush($queueName, $payload);
121
122
    /**
123
     * @param string $queueName
124
     *
125
     * @return string|null
126
     */
127
    abstract protected function lPop($queueName);
128
}
129