AbstractClient   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 62
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A send() 0 7 1
A mapJobs() 0 10 2
1
<?php
2
namespace Phloppy\Client;
3
4
use Phloppy\Job;
5
use Phloppy\RespUtils;
6
use Phloppy\Stream\StreamInterface;
7
use Phloppy\Stream\StreamException;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
11
use Phloppy\Exception\CommandException;
12
13
/**
14
 * Abstract Disque Client.
15
 */
16
abstract class AbstractClient {
17
18
    /**
19
     * @var StreamInterface
20
     */
21
    protected $stream;
22
23
    /**
24
     * @var LoggerInterface
25
     */
26
    protected $log;
27
28
    /**
29
     * @param StreamInterface $stream
30
     * @param LoggerInterface|null $log Logger instance.
31
     */
32 39
    public function __construct(StreamInterface $stream, LoggerInterface $log = null)
33
    {
34 39
        if (!$log) {
35 39
            $log = new NullLogger();
36 39
        }
37
38 39
        $this->log    = $log;
39 39
        $this->stream = $stream;
40 39
    }
41
42
43
    /**
44
     * Send request and retrieve response to the connected disque node.
45
     *
46
     * @param array $args
47
     * @return array|int|null|string
48
     *
49
     * @throws CommandException
50
     * @throws StreamException
51
     */
52 30
    protected function send(array $args = [])
53
    {
54 30
        $this->log->debug('send()ing command', $args);
55 30
        $response = RespUtils::deserialize($this->stream->write(RespUtils::serialize($args)));
56 25
        $this->log->debug('response', [$response]);
57 25
        return $response;
58
    }
59
60
61
    /**
62
     * Map Disque's job responses to Job objects.
63
     *
64
     * @param array $list Job response array from the disque server.
65
     * @return Job[]
66
     */
67 8
    protected function mapJobs(array $list) {
68 8
        return array_map(
69 8
            function($element) { return Job::create([
70 7
                'queue' => $element[0],
71 7
                'id'    => $element[1],
72 7
                'body'  => isset($element[2]) ? $element[2] : '',
73 8
            ]); },
74
            $list
75 8
        );
76
    }
77
}
78