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

RedisClient::set()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
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
19
/**
20
 * Class RedisClient uses the Redis PHP extension.
21
 *
22
 */
23
final class RedisClient implements Cache
24
{
25
    use ClientTrait, MultiplesTrait;
26
27
    private $serializer;
28
29 208
    public function __construct(\Redis $client, Serializer $serializer, int $ttl = null)
30
    {
31 208
        $this->client = $client;
32 208
        $this->serializer = $serializer;
33 208
        $this->ttl = $ttl;
34 208
    }
35
36
37 68
    public function get($key, $default = null)
38
    {
39 68
        return $this->has($key)
40 42
            ? $this->serializer->unserialize($this->client->get($key))
41 51
            : $default;
42
    }
43
44
45 83
    public function set($key, $value, $ttl = null)
46
    {
47 83
        verify_key($key);
48 66
        $expiration = $this->secondsWithGlobalTtl($ttl);
49
50 56
        if (null === $ttl && 0 === $expiration) {
51 52
            return $this->client->set($key, $this->serializer->serialize($value));
52
        }
53
54 5
        if ($expiration > 0) {
55 2
            return $this->client->setex($key, $expiration, $this->serializer->serialize($value));
0 ignored issues
show
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

55
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, $this->serializer->serialize($value));

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\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

55
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, $this->serializer->serialize($value));

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

55
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, $this->serializer->serialize($value));

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

55
            return $this->client->/** @scrutinizer ignore-call */ setex($key, $expiration, $this->serializer->serialize($value));

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...
56
        }
57
58 3
        $this->client->del($key);
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

58
        $this->client->/** @scrutinizer ignore-call */ 
59
                       del($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 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

58
        $this->client->/** @scrutinizer ignore-call */ 
59
                       del($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 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

58
        $this->client->/** @scrutinizer ignore-call */ 
59
                       del($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 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

58
        $this->client->/** @scrutinizer ignore-call */ 
59
                       del($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...
59
60 3
        return true;
61
    }
62
63
64 28
    public function delete($key)
65
    {
66 28
        if (false === $this->has($key)) {
67 8
            return true;
68
        }
69
70 8
        return 1 === $this->client->del($key);
71
    }
72
73
74 206
    public function clear()
75
    {
76 206
        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

76
        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

76
        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

76
        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

76
        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...
77
    }
78
79
80 111
    public function has($key)
81
    {
82 111
        verify_key($key);
83
84 60
        return (bool)$this->client->exists($key);
0 ignored issues
show
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

84
        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\FileClient. ( Ignorable by Annotation )

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

84
        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

84
        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

84
        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...
85
    }
86
}
87