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
Push — master ( c7b958...67ca89 )
by De
01:33
created

Cache::getMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo;
13
14
use Psr\SimpleCache\CacheInterface;
15
16
class Cache implements \Countable, CacheInterface
17
{
18
    const FIELD_NAME_VALUE = 'v';
19
    const FIELD_NAME_EXPIRED = 'e';
20
    const FIELD_NAME_TAGS = 't';
21
    
22
    private $collection;
23
    
24
    public function __construct(Database $database, $collectionName)
25
    {
26
        $this->collection = $database
27
            ->map($collectionName, array(
28
                'index' => array(
29
                    // date field
30
                    array(
31
                        'keys' => array(self::FIELD_NAME_EXPIRED => 1),
32
                        'expireAfterSeconds' => 0
33
                    ),
34
                )
35
            ))
36
            ->getCollection($collectionName)
37
            ->disableDocumentPool();
38
    }
39
40
    /**
41
     * @return Cache
42
     */
43
    public function init()
44
    {
45
        $this->collection->initIndexes();
46
        return $this;
47
    }
48
49
    /**
50
     * @return Cache
51
     */
52
    public function clear()
53
    {
54
        $this->collection->delete();
55
        return $this;
56
    }
57
58
    /**
59
     * @param iterable $keys
60
     * @param mixed|null $default
61
     */
62
    public function getMultiple($keys, $default = null)
63
    {
64
        throw new \Exception('Not implemented');
65
    }
66
67
    /**
68
     * @param iterable $values
69
     * @param null|int|\DateInterval $ttl
70
     */
71
    public function setMultiple($values, $ttl = null)
72
    {
73
        throw new \Exception('Not implemented');
74
    }
75
76
    /**
77
     * @param iterable $keys
78
     */
79
    public function deleteMultiple($keys)
80
    {
81
        throw new \Exception('Not implemented');
82
    }
83
84
    /**
85
     * Set with expiration on concrete date
86
     *
87
     * @param int|string $key
88
     * @param mixed $value
89
     * @param int $timestamp
90
     */
91
    public function setDueDate($key, $value, $timestamp, array $tags = null)
92
    {
93
        $document = array(
94
            '_id' => $key,
95
            self::FIELD_NAME_VALUE => $value,
96
        );
97
98
        if ($timestamp) {
99
            $document[self::FIELD_NAME_EXPIRED] = new \MongoDate((int) $timestamp);
100
        }
101
102
        if ($tags) {
103
            $document[self::FIELD_NAME_TAGS] = $tags;
104
        }
105
106
        $result = $this->collection->getMongoCollection()->update(
107
            array(
108
                '_id' => $key,
109
            ),
110
            $document,
111
            array(
112
                'upsert' => true,
113
            )
114
        );
115
116
        if ((double) 1 !== $result['ok']) {
117
            throw new Exception('Error setting value');
118
        }
119
        
120
        return $this;
121
    }
122
    
123
    /**
124
     * Set key that never expired
125
     *
126
     * @param int|string $key
127
     * @param mixed $value
128
     * @param array $tags
129
     *
130
     * @return Cache
131
     */
132
    public function setNeverExpired($key, $value, array $tags = null)
133
    {
134
        $this->setDueDate($key, $value, null, $tags);
135
        
136
        return $this;
137
    }
138
    
139
    /**
140
     * Set with expiration in seconds
141
     *
142
     * @param int|string $key
143
     * @param mixed $value
144
     * @param int $ttl
145
     * @param array $tags
146
     *
147
     * @return Cache
148
     */
149
    public function set($key, $value, $ttl = null, array $tags = null)
150
    {
151
        $expirationTime = $ttl ? time() + $ttl : null;
152
        $this->setDueDate($key, $value, $expirationTime, $tags);
153
        
154
        return $this;
155
    }
156
157
    /**
158
     * Get value by key
159
     *
160
     * @param string $key
161
     * @param mixed $default
162
     *
163
     * @return array|null
164
     */
165
    public function get($key, $default = null)
166
    {
167
        // Get document
168
        $document = $this->collection->getDocument($key);
169
        if (!$document) {
170
            return $default;
171
        }
172
173
        // Mongo deletes document not exactly when set in field
174
        // Required date checking
175
        // Expiration may be empty for keys whicj never expired
176
        $expiredAt = $document->get(self::FIELD_NAME_EXPIRED);
177
        if ($expiredAt && $expiredAt->sec < time()) {
178
            return $default;
179
        }
180
181
        // Return value
182
        return $document->get(self::FIELD_NAME_VALUE);
183
    }
184
    
185
    public function delete($key)
186
    {
187
        $this->collection->batchDelete(array(
188
            '_id' => $key,
189
        ));
190
        
191
        return $this;
192
    }
193
    
194
    /**
195
     * Delete documents by tag
196
     */
197
    public function deleteMatchingTag($tag)
198
    {
199
        $this->collection->batchDelete(function (\Sokil\Mongo\Expression $e) use ($tag) {
200
            return $e->where(Cache::FIELD_NAME_TAGS, $tag);
201
        });
202
        
203
        return $this;
204
    }
205
    
206
    /**
207
     * Delete documents by tag
208
     */
209
    public function deleteNotMatchingTag($tag)
210
    {
211
        $this->collection->batchDelete(function (\Sokil\Mongo\Expression $e) use ($tag) {
212
            return $e->whereNotEqual(Cache::FIELD_NAME_TAGS, $tag);
213
        });
214
        
215
        return $this;
216
    }
217
    
218
    /**
219
     * Delete documents by tag
220
     * Document deletes if it contains all passed tags
221
     */
222
    public function deleteMatchingAllTags(array $tags)
223
    {
224
        $this->collection->batchDelete(function (\Sokil\Mongo\Expression $e) use ($tags) {
225
            return $e->whereAll(Cache::FIELD_NAME_TAGS, $tags);
226
        });
227
        
228
        return $this;
229
    }
230
    
231
    /**
232
     * Delete documents by tag
233
     * Document deletes if it not contains all passed tags
234
     */
235
    public function deleteMatchingNoneOfTags(array $tags)
236
    {
237
        $this->collection->batchDelete(function (\Sokil\Mongo\Expression $e) use ($tags) {
238
            return $e->whereNoneOf(Cache::FIELD_NAME_TAGS, $tags);
239
        });
240
        
241
        return $this;
242
    }
243
    
244
    /**
245
     * Delete documents by tag
246
     * Document deletes if it contains any of passed tags
247
     */
248
    public function deleteMatchingAnyTag(array $tags)
249
    {
250
        $this->collection->batchDelete(function (\Sokil\Mongo\Expression $e) use ($tags) {
251
            return $e->whereIn(Cache::FIELD_NAME_TAGS, $tags);
252
        });
253
        
254
        return $this;
255
    }
256
    
257
    /**
258
     * Delete documents by tag
259
     * Document deletes if it contains any of passed tags
260
     */
261
    public function deleteNotMatchingAnyTag(array $tags)
262
    {
263
        $this->collection->batchDelete(function (\Sokil\Mongo\Expression $e) use ($tags) {
264
            return $e->whereNotIn(Cache::FIELD_NAME_TAGS, $tags);
265
        });
266
        
267
        return $this;
268
    }
269
270
    /**
271
     * Get total count of documents in cache
272
     *
273
     * @return int
274
     */
275
    public function count()
276
    {
277
        return $this->collection->count();
278
    }
279
    
280
    public function has($key)
281
    {
282
        return (bool) $this->get($key);
283
    }
284
}
285