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

BeanstalkdAdapter::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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