Completed
Push — master ( 8eb315...5aaba6 )
by Changwan
04:29
created

BeanstalkdAdapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 59
ccs 0
cts 20
cp 0
rs 10
wmc 10
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B flush() 0 12 5
A send() 0 4 1
A receive() 0 8 2
A remove() 0 5 1
1
<?php
2
namespace Wandu\Q\Adapter;
3
4
use Pheanstalk\Exception\ServerException;
5
use Pheanstalk\PheanstalkInterface;
6
use Wandu\Q\Contracts\Adapter;
7
8
class BeanstalkdAdapter implements Adapter
9
{
10
    /** @var \Pheanstalk\PheanstalkInterface */
11
    protected $client;
12
    
13
    /** @var string */
14
    protected $channel;
15
    
16
    public function __construct(PheanstalkInterface $client, string $channel = "default")
17
    {
18
        $this->client = $client;
19
        $this->channel = $channel;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function flush()
26
    {
27
        $client = $this->client->useTube($this->channel);
28
        try {
29
            while ($client->delete($client->peekDelayed())) {}
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
30
        } catch (ServerException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
31
        }
32
        try {
33
            while ($client->delete($client->peekReady())) {}
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
34
        } catch (ServerException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
35
        }
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function send(string $payload)
42
    {
43
        $this->client->useTube($this->channel)->put($payload);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function receive()
50
    {
51
        try {
52
            return new BeanstalkdJob($this->client->watch($this->channel)->peekReady());
53
        } catch (ServerException $e) {
54
            return null;
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function remove($job)
62
    {
63
        /** @var \Wandu\Q\Adapter\BeanstalkdJob $job */
64
        return $this->client->delete($job->getJob());
65
    }
66
}
67