Passed
Push — master ( 5a87d9...5af04f )
by Mihail
03:04
created

RedisJsonClient::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Caching\Client;
14
15
use Koded\Caching\Cache;
16
use Koded\Stdlib\Interfaces\Serializer;
17
use function Koded\Caching\verify_key;
18
use function Koded\Stdlib\json_serialize;
19
20
/**
21
 * RedisJsonClient uses the Redis PHP extension.
22
 *
23
 * It will create 2 entries in Redis
24
 * - one as JSON cache item
25
 * - and other as serialized PHP value
26
 *
27
 * The first is useful for other programming languages to use it,
28
 * and the PHP serialized variant is useful only for PHP applications
29
 * where the cached item is handled by PHP serialization.
30
 *
31
 */
32
final class RedisJsonClient implements Cache
33
{
34
    use ClientTrait, MultiplesTrait;
35
36
    private $suffix;
37
    private $options;
38
    private $serializer;
39
40 196
    public function __construct(\Redis $client, Serializer $serializer, int $options, int $ttl = null)
41
    {
42 196
        $this->suffix = '__' . $serializer->type() . '__';
43 196
        $this->serializer = $serializer;
44 196
        $this->options = $options;
45 196
        $this->client = $client;
46 196
        $this->ttl = $ttl;
47 196
    }
48
49
50 64
    public function get($key, $default = null)
51
    {
52 64
        return $this->has($key)
53 39
            ? $this->serializer->unserialize($this->client->get($key . $this->suffix))
54 47
            : $default;
55
    }
56
57
58 76
    public function set($key, $value, $ttl = null)
59
    {
60 76
        verify_key($key);
61 59
        $expiration = $this->secondsWithGlobalTtl($ttl);
62
63 49
        if (null === $ttl && 0 === $expiration) {
64 46
            return $this->client->set($key, json_serialize($value, $this->options))
65 46
                && $this->client->set($key . $this->suffix, $this->serializer->serialize($value));
66
        }
67
68 4
        if ($expiration > 0) {
69 2
            return $this->client->setex($key, $expiration, json_serialize($value, $this->options))
0 ignored issues
show
Bug introduced by
The method setex() does not exist on Koded\Caching\Client\MemoryClient. Did you maybe mean set()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, json_serialize($value, $this->options))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setex() does not exist on Koded\Caching\Client\FileClient. Did you maybe mean set()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, json_serialize($value, $this->options))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setex() does not exist on Memcached. Did you maybe mean set()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, json_serialize($value, $this->options))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setex() does not exist on Koded\Caching\Client\ShmopClient. Did you maybe mean set()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, json_serialize($value, $this->options))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70 2
                && $this->client->setex($key . $this->suffix, $expiration, $this->serializer->serialize($value));
71
        }
72
73 2
        $this->client->del([$key, $key . $this->suffix]);
0 ignored issues
show
Bug introduced by
The method del() does not exist on Koded\Caching\Client\FileClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->client->/** @scrutinizer ignore-call */ 
74
                       del([$key, $key . $this->suffix]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method del() does not exist on Koded\Caching\Client\MemoryClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->client->/** @scrutinizer ignore-call */ 
74
                       del([$key, $key . $this->suffix]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method del() does not exist on Memcached. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->client->/** @scrutinizer ignore-call */ 
74
                       del([$key, $key . $this->suffix]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method del() does not exist on Koded\Caching\Client\ShmopClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->client->/** @scrutinizer ignore-call */ 
74
                       del([$key, $key . $this->suffix]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
75 2
        return true;
76
    }
77
78
79 25
    public function delete($key)
80
    {
81 25
        if (false === $this->has($key)) {
82 6
            return true;
83
        }
84
85 6
        return 2 === $this->client->del([$key, $key . $this->suffix]);
86
    }
87
88
89 196
    public function clear()
90
    {
91 196
        return $this->client->flushDB();
0 ignored issues
show
Bug introduced by
The method flushDB() does not exist on Koded\Caching\Client\FileClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        return $this->client->/** @scrutinizer ignore-call */ flushDB();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method flushDB() does not exist on Koded\Caching\Client\MemoryClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        return $this->client->/** @scrutinizer ignore-call */ flushDB();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method flushDB() does not exist on Memcached. Did you maybe mean flush()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        return $this->client->/** @scrutinizer ignore-call */ flushDB();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method flushDB() does not exist on Koded\Caching\Client\ShmopClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        return $this->client->/** @scrutinizer ignore-call */ flushDB();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
    }
93
94
95 103
    public function has($key)
96
    {
97 103
        verify_key($key);
98
99 52
        return (bool)$this->client->exists($key)
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Koded\Caching\Client\FileClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return (bool)$this->client->/** @scrutinizer ignore-call */ exists($key)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method exists() does not exist on Memcached. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return (bool)$this->client->/** @scrutinizer ignore-call */ exists($key)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method exists() does not exist on Koded\Caching\Client\MemoryClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return (bool)$this->client->/** @scrutinizer ignore-call */ exists($key)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method exists() does not exist on Koded\Caching\Client\ShmopClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return (bool)$this->client->/** @scrutinizer ignore-call */ exists($key)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100 52
            && (bool)$this->client->exists($key . $this->suffix);
101
    }
102
}
103