Completed
Pull Request — master (#50)
by Dominic
02:22
created

Client::connect()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Disque;
3
4
use Disque\Command;
5
use Disque\Command\CommandInterface;
6
use Disque\Command\InvalidCommandException;
7
use Disque\Connection\Manager;
8
use Disque\Connection\ManagerInterface;
9
use Disque\Queue\Queue;
10
11
/**
12
 * @method int ackJob(string... $ids)
13
 * @method string addJob(string $queue, string $payload, array $options = [])
14
 * @method int delJob(string... $ids)
15
 * @method int dequeue(string... $ids)
16
 * @method int enqueue(string... $ids)
17
 * @method int fastAck(string... $ids)
18
 * @method array getJob(string... $queues, array $options = [])
19
 * @method array hello()
20
 * @method string info()
21
 * @method array jscan(array $options = [])
22
 * @method int nack(string... $ids)
23
 * @method int qlen(string $queue)
24
 * @method array qpeek(string $queue, int $count)
25
 * @method array qscan(array $options = [])
26
 * @method array show(string $id)
27
 * @method int working(string $id)
28
 */
29
class Client
30
{
31
    /**
32
     * Connection manager
33
     *
34
     * @var ManagerInterface
35
     */
36
    protected $connectionManager;
37
38
    /**
39
     * Command handlers
40
     *
41
     * @var array
42
     */
43
    protected $commandHandlers = [];
44
45
    /**
46
     * List of built queues
47
     *
48
     * @var array
49
     */
50
    private $queues;
51
52
    /**
53
     * A list of credentials to Disque servers
54
     *
55
     * @var Disque\Connection\Credentials[]
56
     */
57
    private $servers;
58
59
    /**
60
     * Create a new Client
61
     *
62
     * @param Disque\Connection\Credentials[] $servers
63
     */
64 18
    public function __construct(array $servers = [])
65
    {
66
        foreach ([
67 18
            new Command\AckJob(),
68 18
            new Command\AddJob(),
69 18
            new Command\DelJob(),
70 18
            new Command\Dequeue(),
71 18
            new Command\Enqueue(),
72 18
            new Command\FastAck(),
73 18
            new Command\GetJob(),
74 18
            new Command\Hello(),
75 18
            new Command\Info(),
76 18
            new Command\JScan(),
77 18
            new Command\Nack(),
78 18
            new Command\Pause(),
79 18
            new Command\QLen(),
80 18
            new Command\QPeek(),
81 18
            new Command\QScan(),
82 18
            new Command\QStat(),
83 18
            new Command\Show(),
84 18
            new Command\Working()
85 18
        ] as $command) {
86 18
            $this->registerCommand($command);
87 18
        }
88
89 18
        $this->servers = $servers;
90
91 18
        $connectionManager = new Manager();
92 18
        $this->setConnectionManager($connectionManager);
93 18
    }
94
95
    /**
96
     * Set a connection manager
97
     *
98
     * @param ManagerInterface $manager
99
     */
100 11
    public function setConnectionManager(ManagerInterface $manager)
101
    {
102 11
        $this->connectionManager = $manager;
103 11
        foreach ($this->servers as $server) {
104 1
            $this->connectionManager->addServer($server);
105 11
        }
106 11
    }
107
108
    /**
109
     * Get the connection manager
110
     *
111
     * @return ManagerInterface Connection manager
112
     */
113 3
    public function getConnectionManager()
114
    {
115 3
        return $this->connectionManager;
116
    }
117
118
    /**
119
     * Tells if connection is established
120
     *
121
     * @return bool Success
122
     */
123 1
    public function isConnected()
124
    {
125 1
        return $this->connectionManager->isConnected();
126
    }
127
128
    /**
129
     * Connect to Disque
130
     *
131
     * @return Disque\Connection\Node\Node Connected node information
132
     * @throws Disque\Connection\ConnectionException
133
     */
134 2
    public function connect()
135
    {
136 2
        return $this->connectionManager->connect();
137
    }
138
139
    /**
140
     * @throws InvalidCommandException
141
     */
142 4
    public function __call($command, array $arguments)
143
    {
144 4
        $command = strtoupper($command);
145 4
        if (!isset($this->commandHandlers[$command])) {
146 2
            throw new InvalidCommandException($command);
147
        }
148
149 2
        $command = $this->commandHandlers[$command];
150 2
        $command->setArguments($arguments);
151 2
        $result = $this->connectionManager->execute($command);
152 1
        return $command->parse($result);
153
    }
154
155
    /**
156
     * Register a command handler
157
     *
158
     * @param CommandInterface $commandHandler Command
159
     * @return void
160
     */
161 18
    public function registerCommand(CommandInterface $commandHandler)
162
    {
163 18
        $command = strtoupper($commandHandler->getCommand());
164 18
        $this->commandHandlers[$command] = $commandHandler;
165 18
    }
166
167
    /**
168
     * Get a queue
169
     *
170
     * @param string $name Queue name
171
     * @return Queue Queue
172
     */
173 3
    public function queue($name)
174
    {
175 3
        if (!isset($this->queues[$name])) {
176 3
            $this->queues[$name] = new Queue($this, $name);
177 3
        }
178 3
        return $this->queues[$name];
179
    }
180
}