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

BeanstalkdAdapter::flush()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 16
nop 0
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 30
rs 8.8571
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