Completed
Push — feature/0.7.0 ( b421e8...de8513 )
by Ryuichi
03:07
created

Memcached::verifyReturnCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Cache\Driver;
3
4
use WebStream\DI\Injector;
5
use WebStream\Module\Container;
6
7
/**
8
 * Memcached
9
 * @author Ryuichi Tanaka
10
 * @since 2015/07/08
11
 * @version 0.7
12
 */
13
class Memcached implements ICache
14
{
15
    use Injector;
16
17
    /**
18
     * @var \Memcached memcachedオブジェクト
19
     */
20
    private $cache;
21
22
    /**
23
     * @var bool キャッシュ使用可能フラグ
24
     */
25
    private $isAvairable;
26
27
    /**
28
     * @var string キャッシュ接頭辞
29
     */
30
    private $cachePrefix;
31
32
    /**
33
     * constructor
34
     */
35
    public function __construct(Container $container)
36
    {
37
        $this->isAvairable = extension_loaded('memcached');
38
        $this->cachePrefix = $container->cachePrefix;
0 ignored issues
show
Documentation introduced by
The property cachePrefix does not exist on object<WebStream\Module\Container>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
40
        if ($this->isAvairable) {
41
            $this->cache = new \Memcached();
42
            $this->cache->addServers($container->servers);
0 ignored issues
show
Documentation introduced by
The property servers does not exist on object<WebStream\Module\Container>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
44
            $defaultOptions = [
45
                \Memcached::OPT_CONNECT_TIMEOUT => 50,
46
                \Memcached::OPT_RETRY_TIMEOUT => 50,
47
                \Memcached::OPT_SEND_TIMEOUT => 50,
48
                \Memcached::OPT_RECV_TIMEOUT => 50,
49
                \Memcached::OPT_POLL_TIMEOUT => 50,
50
                \Memcached::OPT_COMPRESSION => true,
51
                \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
52
                \Memcached::OPT_BINARY_PROTOCOL => true
53
            ];
54
55
            if (\Memcached::HAVE_IGBINARY) {
56
                $defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY;
57
            }
58
59
            $this->cache->setOptions($defaultOptions);
0 ignored issues
show
Bug introduced by
The method setOptions() does not exist on Memcached. Did you maybe mean setOption()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function add($key, $value, $ttl = 0, $overwrite = false): bool
67
    {
68
        if (!$this->isAvailableCacheLibrary()) {
69
            return false;
70
        }
71
        $key = $this->cachePrefix . $key;
72
        $result = null;
73
74
        if ($ttl > 0) {
75 View Code Duplication
            if ($overwrite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                if ($this->cache->replace($key, $value, $ttl) === false) {
77
                    $result = $this->cache->set($key, $value, $ttl);
78
                }
79
            } else {
80
                $result = $this->cache->set($key, $value, $ttl);
81
            }
82
        } else {
83 View Code Duplication
            if ($overwrite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                if ($this->cache->replace($key, $value) === false) {
85
                    $result = $this->cache->set($key, $value);
86
                }
87
            } else {
88
                $result = $this->cache->set($key, $value);
89
            }
90
        }
91
92
        $this->logger->info("Execute cache save: " . $key);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
        $this->verifyReturnCode(\Memcached::RES_SUCCESS);
94
95
        return $result;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 View Code Duplication
    public function get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        if (!$this->isAvailableCacheLibrary()) {
104
            return false;
105
        }
106
        $key = $this->cachePrefix . $key;
107
        $value = $this->cache->get($key);
108
        $this->logger->info("Execute cache read: " . $key);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
110
        return $this->verifyReturnCode(\Memcached::RES_SUCCESS) ? $value : null;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 View Code Duplication
    public function delete($key): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        if (!$this->isAvailableCacheLibrary()) {
119
            return false;
120
        }
121
        $key = $this->cachePrefix . $key;
122
        $this->cache->delete($key);
123
        $this->logger->info("Execute cache cleared: " . $key);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
125
        return $this->verifyReturnCode(\Memcached::RES_NOTFOUND);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function clear(): bool
132
    {
133
        if (!$this->isAvailableCacheLibrary()) {
134
            return false;
135
        }
136
        $allKeys = $this->cache->getAllKeys();
137
        if ($allKeys === false) {
138
            $this->logger->warn();
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
            $this->cache->flush();
140
141
            return true;
142
        }
143
144
        $prefixLength = strlen($this->cachePrefix);
145
        $targetKeys = [];
146
        foreach ($allKeys as $key) {
147
            if (substr($key, 0, $prefixLength)) {
148
                $targetKeys[] = $key;
149
            }
150
        }
151
152
        $this->deleteMulti($targetKeys);
0 ignored issues
show
Bug introduced by
The method deleteMulti() does not exist on WebStream\Cache\Driver\Memcached. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
153
        $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*");
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
154
155
        return $this->verifyReturnCode(\Memcached::RES_NOTFOUND);
156
    }
157
158
    /**
159
     * リターンコードを検証する
160
     * @param int $code 想定コード
161
     * @return bool 検証結果
162
     */
163
    private function verifyReturnCode(int $code)
164
    {
165
        if ($code !== $this->cache->getResultCode()) {
166
            $message = $this->cache->getResultMessage();
167
            $this->logger->warn("Error $code interacting with memcached: $message");
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
169
            return false;
170
        }
171
172
        return true;
173
    }
174
175
    /**
176
     * キャッシュライブラリが使用可能か検査する
177
     * @return bool 使用可能でtrue
178
     */
179
    private function isAvailableCacheLibrary(): bool
180
    {
181
        if ($this->isAvairable) {
182
            return true;
183
        }
184
185
        $this->logger->warn("Memcached cache library is unavailable.");
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Memcached>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
186
187
        return false;
188
    }
189
}
190