BeanstalkdDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 64
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A push() 0 5 1
A pop() 0 20 4
A release() 0 4 1
A delete() 0 4 1
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