Completed
Push — master ( a4fd5b...a90998 )
by Ryan
03:03
created

Service::getSetGetBatch()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 33.0036

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 7
cts 21
cp 0.3333
rs 4.9091
cc 9
eloc 18
nc 15
nop 3
crap 33.0036
1
<?php
2
/**
3
 * Opine\Cache
4
 *
5
 * Copyright (c)2013, 2014 Ryan Mahoney, https://github.com/Opine-Org <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
namespace Opine\Cache;
26
27
use Memcache;
28
use Exception;
29
use Closure;
30
use Opine\Interfaces\Cache as CacheInterface;
31
use Symfony\Component\Yaml\Yaml;
32
33
class Service implements CacheInterface
34
{
35
    private $memcache;
36
    private $host = false;
37
    private $port = false;
38
    private $root;
39
40 8
    public function __construct($root)
41
    {
42 8
        $this->root = $root;
43
44
        // determine config environment
45 8
        $environment = 'default';
46 8
        $test = getenv('OPINE_ENV');
47 8
        if ($test !== false) {
48
            $environment = $test;
49
        }
50 8
        if ($environment == 'default') {
51 8
            $environment = '.';
52 8
        }
53
54
        // determine path of file
55 8
        $path = $root . '/../config/settings/' . $environment . '/cache.yml';
56 8
        if (!file_exists($path) && $environment != '.') {
57
            $path = $root . '/../config/settings/cache.yml';
58
        }
59 8
        if (!file_exists($path)) {
60 8
            return;
61
        }
62
63
        // read configuration
64
        $config = Yaml::parse(file_get_contents($path));
65
66
        $this->host = $config['settings']['host'];
67
        $this->port = $config['settings']['port'];
68
        if (!$this->check()) {
69
            return;
70
        }
71
        $this->memcache = new Memcache();
72
    }
73
74 7
    private function check()
75
    {
76 7
        if (!class_exists('Memcache')) {
77 7
            return false;
78
        }
79
        if ($this->host === false) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->host === false);.
Loading history...
80
            return false;
81
        }
82
        return true;
83
    }
84
85 1
    public function delete($key, $timeout = 0)
86
    {
87 1
        if (!$this->check()) {
88 1
            return false;
89
        }
90
        $result = @$this->memcache->pconnect($this->host, $this->port);
91
        if ($result === false) {
92
            return false;
93
        }
94
95
        return $this->memcache->delete($key, $timeout);
96
    }
97
98 1
    public function set($key, $value, $expire = 0, $flag = 2)
99
    {
100 1
        if (!$this->check()) {
101 1
            return false;
102
        }
103
        $result = @$this->memcache->pconnect($this->host, $this->port);
104
        if ($result === false) {
105
            return false;
106
        }
107
108
        return $this->memcache->set($key, $value, $flag, $expire);
109
    }
110
111 2
    public function get($key, $flag = 2)
112
    {
113 2
        if (!$this->check()) {
114 2
            return false;
115
        }
116
        $result = @$this->memcache->pconnect($this->host, $this->port);
117
        if ($result === false) {
118
            return false;
119
        }
120
121
        return $this->memcache->get($key, $flag);
122
    }
123
124 1
    public function getSetGet($key, Closure $callback, $ttl = 0, $flag = 2)
125
    {
126 1
        if (!$this->check()) {
127 1
            return $callback();
128
        }
129
        $result = @$this->memcache->pconnect($this->host, $this->port);
130
        if ($result === false) {
131
            return false;
132
        }
133
        $data = $this->memcache->get($key, $flag);
134
        if ($data === false) {
135
            $data = $callback();
136
            if ($data !== false) {
137
                $this->memcache->set($key, $data, $flag, $ttl);
138
            }
139
        }
140
141
        return $data;
142
    }
143
144 2
    public function getSetGetBatch(Array &$items, $ttl = 0, $flag = 2)
145
    {
146 2
        foreach ($items as $item) {
147 2
            if (!is_callable($item)) {
148 1
                throw new Exception('each item must have a callback defined');
149
            }
150 1
        }
151 1
        if (!$this->check()) {
152 1
            return false;
153
        }
154
        $result = @$this->memcache->pconnect($this->host, $this->port);
155
        if ($result === false) {
156
            return false;
157
        }
158
        $data = $this->memcache->get(array_keys($items));
159
        foreach ($items as $key => &$item) {
160
            if (!isset($data[$key]) || $data[$key] === false) {
161
                $items[$key] = $item();
162
            } else {
163
                $items[$key] = $data[$key];
164
            }
165
            if ($items[$key] !== false) {
166
                $this->memcache->set($key, $items[$key], $flag, $ttl);
167
            }
168
        }
169
170
        return true;
171
    }
172
173 1
    public function getBatch(Array &$items, $flag = 2)
174
    {
175 1
        if (!$this->check()) {
176 1
            return false;
177
        }
178
        $count = sizeof($items);
179
        $result = @$this->memcache->pconnect($this->host, $this->port);
180
        if ($result === false) {
181
            return false;
182
        }
183
        $data = $this->memcache->get(array_keys($items), $flag);
184
        $hits = 0;
185
        foreach ($items as $key => $item) {
186
            if (array_key_exists($key, $data)) {
187
                $items[$key] = $data[$key];
188
                $hits++;
189
            }
190
        }
191
        return $hits == $count;
192
    }
193
194 1
    public function deleteBatch(Array $items, $timeout = 0)
195
    {
196 1
        if (!$this->check()) {
197 1
            return false;
198
        }
199
        $result = @$this->memcache->pconnect($this->host, $this->port);
200
        if ($result === false) {
201
            return false;
202
        }
203
        foreach ($items as $item) {
204
            $this->memcache->delete($item, $timeout);
205
        }
206
207
        return true;
208
    }
209
}
210