GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch v2 (777d15)
by Hilari
02:30
created

RedisCache   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 78.38%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
c 2
b 1
f 0
lcom 1
cbo 3
dl 0
loc 131
ccs 29
cts 37
cp 0.7838
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 8 2
A setItems() 0 8 2
A has() 0 4 1
A demand() 0 10 2
A get() 0 8 2
A getItems() 0 9 2
A delete() 0 4 1
A deleteAll() 0 4 1
A flush() 0 4 1
A getTimeToLive() 0 6 3
1
<?php
2
3
namespace Cmp\Cache\Backend;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\NotFoundException;
7
use Cmp\Cache\TimeToLiveAwareCache;
8
use Cmp\Cache\Traits\MultiCacheTrait;
9
use Redis;
10
11
/**
12
 * Class RedisCache
13
 * 
14
 * A redis powered backend for caching
15
 *
16
 * @package Cmp\Cache\Infrastureture\Backend
17
 */
18
class RedisCache implements Cache
19
{
20
    use MultiCacheTrait {
21
        MultiCacheTrait::setItems as setItemsTrait;
22
    }
23
24
    const DEFAULT_HOST    = '127.0.0.1';
25
    const DEFAULT_PORT    = 6379;
26
    const DEFAULT_DB      = 0;
27
    const DEFAULT_TIMEOUT = 0.0;
28
29
    /**
30
     * @var Redis
31
     */
32
    private $client;
33
34
    /**
35
     * RedisCache constructor.
36
     *
37
     * @param Redis $client
38
     */
39 1
    public function __construct(Redis $client)
40
    {
41 1
        $this->client = $client;
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function set($key, $value, $timeToLive = null)
48
    {
49 1
        if ($timeToLive > 0) {
50 1
            return $this->client->setex($key, $timeToLive, $value);
51
        }
52
53 1
        return $this->client->set($key, $value);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function setItems(array $items, $timeToLive = null)
60
    {
61 1
        if (!$timeToLive) {
62 1
            return $this->client->mset($items);
63
        }
64
65 1
        return $this->setItemsTrait($items, $timeToLive);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function has($key)
72
    {
73 1
        return $this->client->exists($key);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function demand($key)
80
    {
81 1
        $value = $this->client->get($key);
82
83 1
        if (!$value) {
84 1
            throw new NotFoundException($key);
85
        }
86
87 1
        return $value;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function get($key, $default = null)
94
    {
95
        try {
96 1
            return $this->demand($key);
97 1
        } catch (NotFoundException $exception) {
98 1
            return $default;
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getItems(array $keys)
106
    {
107
        $items = $this->client->mget($keys);
108
        array_walk($items, function(&$value) {
109
            $value = $value === false ? null : $value;
110
        });
111
112
        return $items;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function delete($key)
119
    {
120 1
        return (bool) $this->client->delete($key);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function deleteAll(array $keys)
127
    {
128
        return (bool) call_user_func_array([$this->client, 'delete', $keys]);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function flush()
135
    {
136 1
        return $this->client->flushDB();
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function getTimeToLive($key)
143
    {
144 1
        $timeToLive = $this->client->ttl($key);
145
146 1
        return false === $timeToLive || $timeToLive <= 0 ? null : $timeToLive; 
147
    }
148
}
149