PredisAdapter::isAvailable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
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