Node   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 4
dl 0
loc 131
ccs 48
cts 48
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A auth() 0 5 1
A ping() 0 4 1
B info() 0 24 2
B hello() 0 29 5
A del() 0 9 2
A jscan() 0 10 1
1
<?php
2
namespace Phloppy\Client;
3
4
use Iterator;
5
use Phloppy\Client\Node\JScanIterator;
6
use Phloppy\Exception\CommandException;
7
use Phloppy\NodeInfo;
8
9
/**
10
 * Disque commands for local nodes.
11
 */
12
class Node extends AbstractClient {
13
14
    /**
15
     * Authenticate against a Disque node.
16
     *
17
     * @param $password
18
     * @return boolean true if authenticated. False otherwise.
19
     *
20
     * @throws CommandException
21
     */
22 2
    public function auth($password)
0 ignored issues
show
Coding Style introduced by
function auth() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
23
    {
24 2
        $rsp = $this->send(['AUTH', $password]);
25 1
        return $rsp === 'OK';
26
    }
27
28
29
    /**
30
     * Send a ping command to the connected server.
31
     *
32
     * @return boolean True if the ping is acknowleged. False otherwise.
33
     */
34 2
    public function ping()
0 ignored issues
show
Coding Style introduced by
function ping() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
35
    {
36 2
        return $this->send(['PING']) === 'PONG';
37
    }
38
39
40
    /**
41
     * @return array
42
     */
43 1
    public function info()
44
    {
45 1
        $rsp      = $this->send(['INFO']);
46 1
        $info     = [];
47 1
        $sections = preg_split('/^#/m', $rsp);
48
49 1
        foreach ($sections as $section) {
50 1
            $lines  = explode("\r\n", trim($section));
51 1
            $header = trim($lines[0]);
52
53 1
            array_shift($lines);
54
55 1
            $lines = array_reduce($lines, function($c, $e) {
56 1
                list($k, $v) = explode(':', $e);
57 1
                $c[$k]       = $v;
58
59 1
                return $c;
60 1
            }, []);
61
62 1
            $info[$header] = $lines;
63 1
        }
64
65 1
        return $info;
66
    }
67
68
69
    /**
70
     * @return NodeInfo[]
71
     */
72 2
    public function hello()
73
    {
74 2
        $nodes    = [];
75 2
        $response = $this->send(['HELLO']);
76 2
        $version  = array_shift($response);
77
78
        switch ($version) {
79 2
            case 1:
80
                // active node id
81 2
                array_shift($response);
82
83
                // in single node mode the server may not have
84
                // determinded its own IP address, so we just use the
85
                // node url that we used to connect.
86 2
                if (count($response === 1) && $response[0][1] === '') {
87 2
                    $host           = parse_url($this->stream->getNodeUrl(), PHP_URL_HOST);
88 2
                    $response[0][1] = $host;
89 2
                }
90
91 2
                foreach ($response as $node) {
92 2
                    $server  = 'tcp://'.$node[1].':'.$node[2];
93 2
                    $nodes[] = new NodeInfo($node[0], $server, $node[3]);
94 2
                }
95
96 2
                break;
97
        }
98
99 2
        return $nodes;
100
    }
101
102
103
    /**
104
     * DELETE jobs from the connected node.
105
     *
106
     * @param string[] $jobs Job IDs to delete.
107
     *
108
     * @return int Number of jobs deleted
109
     */
110 3
    public function del(array $jobs)
111
    {
112
        try {
113 3
            return (int) $this->send(array_merge(['DELJOB'], $jobs));
114 1
        } catch (CommandException $e) {
115 1
            return 0;
116
        }
117
118
    }
119
120
121
    /**
122
     * Retrieve an Iterator over available jobs on the connected Disque node.
123
     *
124
     * @param int        $count
125
     * @param array      $queues
126
     * @param array      $states
127
     * @param            $format
128
     *
129
     * @return Iterator
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use JScanIterator.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
130
     * @see https://github.com/antirez/disque#jscan-cursor-count-count-busyloop-queue-queue-state-state1-state-state2--state-staten-reply-allid
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
131
     */
132 1
    public function jscan($count = 50, array $queues = [], array $states = [], $format = JScanIterator::FORMAT_ID)
133
    {
134 1
        $iterator = new JScanIterator($this->stream, $this->log);
135 1
        $iterator->setCount($count);
136 1
        $iterator->setQueues($queues);
137 1
        $iterator->setStates($states);
138 1
        $iterator->setFormat($format);
139
140 1
        return $iterator;
141
    }
142
}
143