Completed
Pull Request — master (#16)
by Jan
20:37 queued 18:19
created

AbstractClient::getDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Phloppy\Client;
3
4
use Phloppy\Exception\CommandException;
5
use Phloppy\Job;
6
use Phloppy\RespUtils;
7
use Phloppy\Stream\StreamException;
8
use Phloppy\Stream\StreamInterface;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use Symfony\Component\EventDispatcher\EventDispatcher;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
14
15
/**
16
 * Abstract Disque Client.
17
 */
18
abstract class AbstractClient {
19
20
    /**
21
     * @var StreamInterface
22
     */
23
    protected $stream;
24
25
    /**
26
     * @var LoggerInterface
27
     */
28
    protected $log;
29
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    private $dispatcher;
34
35
36
    /**
37
     * @param StreamInterface      $stream
38
     * @param LoggerInterface|null $log Logger instance.
39
     */
40 39
    public function __construct(
41
        StreamInterface $stream,
42
        LoggerInterface $log = null,
43
        EventDispatcherInterface $dispatcher = null
44
    ) {
45 39
        if (!$log) {
46 39
            $log = new NullLogger();
47 39
        }
48
49 39
        if (!$dispatcher) {
50 39
            $dispatcher = new EventDispatcher();
51 39
        }
52
53 39
        $this->log = $log;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54 39
        $this->stream = $stream;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55 39
        $this->dispatcher = $dispatcher;
56 39
    }
57
58
59
    /**
60
     * @return EventDispatcherInterface
61
     */
62 7
    public function getDispatcher()
63
    {
64 7
        return new $this->dispatcher;
65
    }
66
67
68
    /**
69
     * Send request and retrieve response to the connected disque node.
70
     *
71
     * @param array $args
72
     *
73
     * @return array|int|null|string
74
     *
75
     * @throws CommandException
76
     * @throws StreamException
77
     */
78 30
    protected function send(array $args = [])
79
    {
80 30
        $this->log->debug('send()ing command', $args);
81 30
        $response = RespUtils::deserialize($this->stream->write(RespUtils::serialize($args)));
82 25
        $this->log->debug('response', [$response]);
83
84 25
        return $response;
85
    }
86
87
88
    /**
89
     * Map Disque's job responses to Job objects.
90
     *
91
     * @param array $list Job response array from the disque server.
92
     *
93
     * @return Job[]
94
     */
95 8
    protected function mapJobs(array $list)
96
    {
97 8
        return array_map(
98 8
            function ($element) {
99 7
                return Job::create([
100 7
                    'queue' => $element[0],
101 7
                    'id' => $element[1],
102 7
                    'body' => isset($element[2]) ? $element[2] : '',
103 7
                ]);
104 8
            },
105
            $list
106 8
        );
107
    }
108
}
109