Test Failed
Push — master ( 16b6ce...8a8e9e )
by Terry
09:54
created

Memcache::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache\Driver;
14
15
use Shieldon\SimpleCache\CacheProvider;
16
use Shieldon\SimpleCache\Exception\CacheException;
17
use Memcache as MemcacheServer;
18
use Exception;
19
use function array_keys;
20
use function unserialize;
21
use function serialize;
22
23
/**
24
 * A cache driver class provided by Memcache
25
 * 
26
 * @see https://www.php.net/manual/en/book.memcache.php
27
 */
28
class Memcache extends CacheProvider
29
{
30
    /**
31
     * The Memcache instance.
32
     *
33
     * @var Memcache|null
34
     */
35
    protected $memcache = null;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param array $setting The settings.
41
     * 
42
     * @throws CacheException
43
     */
44
    public function __construct(array $setting = [])
45
    {
46
        $config = [
47
            'host' => '127.0.0.1',
48
			'port' => 11211,
49
        ];
50
51
        foreach (array_keys($config) as $key) {
52
            if (isset($setting[$key])) {
53
                $config[$key] = $setting[$key];
54
            }
55
        }
56
57
        $this->connect($config);
58
    }
59
60
    /**
61
     * Connect to Memchaced server.
62
     *
63
     * @param array $config The settings.
64
     * 
65
     * @return void
66
     * 
67
     * @throws CacheException
68
     */
69
    protected function connect(array $config): void
70
    {
71
        if (extension_loaded('memcache')) {
72
            try {
73
                $this->memcache = new MemcacheServer();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Memcache() of type Memcache is incompatible with the declared type Shieldon\SimpleCache\Driver\Memcache|null of property $memcache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
                $this->memcache->addServer(
75
					$config['host'],
76
                    $config['port'],
77
                    true,
78
					1
79
				);
80
            // @codeCoverageIgnoreStart
81
            } catch (Exception $e) {
82
                throw new CacheException($e->getMessage());
83
            }
84
            // @codeCoverageIgnoreEnd
85
            return;
86
        }
87
88
        // @codeCoverageIgnoreStart
89
        throw new CacheException(
90
            'PHP Memcache extension is not installed on your system.'
91
        );
92
        // @codeCoverageIgnoreEnd
93
    }
94
95
    /**
96
     * Fetch a cache by an extended Cache Driver.
97
     *
98
     * @param string $key The key of a cache.
99
     *
100
     * @return array
101
     */
102
    protected function doGet(string $key): array
103
    {
104
        $content = $this->memcache->get($key);
1 ignored issue
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

104
        /** @scrutinizer ignore-call */ 
105
        $content = $this->memcache->get($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...
105
106
        if (empty($content)) {
107
            return [];
108
        }
109
        $data = unserialize($content);
110
111
        return $data;
112
    }
113
114
    /**
115
     * Set a cache by an extended Cache Driver.
116
     *
117
     * @param string $key       The key of a cache.
118
     * @param mixed  $value     The value of a cache. (serialized)
119
     * @param int    $ttl       The time to live for a cache.
120
     * @param int    $timestamp The time to store a cache.
121
     *
122
     * @return bool
123
     */
124
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
125
    {
126
        $contents = [
127
            'timestamp' => $timestamp,
128
            'ttl'       => $ttl,
129
            'value'     => $value
130
        ];
131
132
        $result = $this->memcache->set(
133
            $key,
134
            serialize($contents),
135
            0,
136
            $ttl
0 ignored issues
show
Unused Code introduced by
The call to Shieldon\SimpleCache\CacheProvider::set() has too many arguments starting with $ttl. ( Ignorable by Annotation )

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

136
        /** @scrutinizer ignore-call */ 
137
        $result = $this->memcache->set(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
137
        );
138
139
        return $result;
140
    }
141
142
    /**
143
     * Delete a cache by an extended Cache Driver.
144
     *
145
     * @param string $key The key of a cache.
146
     * 
147
     * @return bool
148
     */
149
    protected function doDelete(string $key): bool
150
    {
151
        return $this->memcache->delete($key);
152
    }
153
154
    /**
155
     * Delete all caches by an extended Cache Driver.
156
     * 
157
     * @return bool
158
     */
159
    protected function doClear(): bool
160
    {
161
        return $this->memcache->flush();
0 ignored issues
show
Bug introduced by
The method flush() does not exist on Shieldon\SimpleCache\Driver\Memcache. ( Ignorable by Annotation )

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

161
        return $this->memcache->/** @scrutinizer ignore-call */ flush();

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...
162
    }
163
164
    /**
165
     * Check if the cache exists or not.
166
     *
167
     * @param string $key The key of a cache.
168
     *
169
     * @return bool
170
     */
171
    protected function doHas(string $key): bool
172
    {
173
        $content = $this->memcache->get($key);
174
175
        if (empty($content)) {
176
            return false;
177
        }
178
        return true;
179
    }
180
}
181