Completed
Push — master ( 4c1e33...23763c )
by Mariano
02:50
created

Predis::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
namespace Disque\Connection;
3
4
use Disque\Command\CommandInterface;
5
use Predis\Client as PredisClient;
6
7
class Predis extends BaseConnection implements ConnectionInterface
8
{
9
    /**
10
     * Client
11
     *
12
     * @var \Predis\Client
13
     */
14
    protected $client;
15
16
    /**
17
     * @inheritdoc
18
     */
19 1
    public function connect($connectionTimeout = null, $responseTimeout = null)
20
21
    {
22 1
        parent::connect($connectionTimeout, $responseTimeout);
23
24 1
        $this->client = $this->buildClient($this->host, $this->port);
25 1
        $this->client->connect();
26 1
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 8
    public function disconnect()
32
    {
33 8
        if (!$this->isConnected()) {
34 8
            return;
35
        }
36 1
        $this->client->disconnect();
37 1
        $this->client = null;
38 1
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 8
    public function isConnected()
44
    {
45 8
        return (isset($this->client) && $this->client->isConnected());
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 3
    public function execute(CommandInterface $command)
52
    {
53 3
        if (!$this->isConnected()) {
54 1
            throw new ConnectionException('No connection established');
55
        }
56 2
        return $this->client->executeRaw(array_merge(
57 2
            [$command->getCommand()],
58 2
            $command->getArguments()
59 2
        ));
60
    }
61
62
    /**
63
     * Build Predis client
64
     *
65
     * @param string $host Host
66
     * @param int $port Port
67
     * @return Predis\Client Client
68
     */
69
    protected function buildClient($host, $port)
70
    {
71
        return new PredisClient(['scheme' => 'tcp'] + compact('host', 'port'));
72
    }
73
}
74