PredisAdapter::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 3
eloc 8
nc 6
nop 2
1
<?php
2
3
namespace NilPortugues\Cache\Adapter;
4
5
use NilPortugues\Cache\Adapter\Redis\AbstractAdapter;
6
use NilPortugues\Cache\CacheAdapter;
7
use Predis\Client;
8
9
/**
10
 * Class PredisAdapter
11
 * @package NilPortugues\Cache\Adapter\Redis
12
 */
13
class PredisAdapter extends AbstractAdapter
14
{
15
    /**
16
     * @param array           $connections
17
     * @param CacheAdapter    $next
18
     */
19
    public function __construct(array $connections, CacheAdapter $next = null)
20
    {
21
        $this->nextAdapter     = (InMemoryAdapter::getInstance() === $next) ? null: $next;
22
23
        try {
24
            $this->connected = true;
25
            $this->redis = new Client($connections);
26
            $this->redis->connect();
27
        } catch (\Exception $e) {
28
            $this->connected = false;
29
        }
30
    }
31
32
    /**
33
     * Checks the availability of the cache service.
34
     *
35
     * @return bool
36
     */
37
    public function isAvailable()
38
    {
39
        return $this->connected && $this->redis->isConnected();
40
    }
41
}
42