AbstractClient::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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