BeanstalkdDriver::pop()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Zenstruck\Queue\Driver;
4
5
use Pheanstalk\Job as PheanstalkJob;
6
use Pheanstalk\PheanstalkInterface;
7
use Zenstruck\Queue\Driver;
8
use Zenstruck\Queue\Job;
9
use Zenstruck\Queue\Payload;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class BeanstalkdDriver implements Driver
15
{
16
    private $client;
17
    private $tube;
18
19
    /**
20
     * @param PheanstalkInterface $client
21
     * @param string              $tube
22
     */
23 1
    public function __construct(PheanstalkInterface $client, $tube)
24
    {
25 1
        $this->client = $client;
26 1
        $this->tube = $tube;
27 1
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function push(Payload $payload)
33
    {
34 1
        $this->client->useTube($this->tube);
35 1
        $this->client->put(json_encode($payload));
36 1
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function pop()
42
    {
43 1
        $rawJob = $this->client->watchOnly($this->tube)->reserve(0);
44
45 1
        if (!$rawJob instanceof PheanstalkJob) {
46 1
            return null;
47
        }
48
49 1
        if (null === $payload = Payload::fromJson($rawJob->getData())) {
50
            // can't handle - requeue
51 1
            $this->client->release($rawJob, 2048);
52
53 1
            return null;
54
        }
55
56 1
        $stats = $this->client->statsJob($rawJob);
57 1
        $attempts = isset($stats['reserves']) ? (int) $stats['reserves'] : 1;
58
59 1
        return new Job($payload, $attempts, $rawJob);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function release(Job $job)
66
    {
67 1
        $this->client->release($job->id());
68 1
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1
    public function delete(Job $job)
74
    {
75 1
        $this->client->delete($job->id());
76 1
    }
77
}
78