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

Apcu::isAvailableCacheLibrary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
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
 * Apcu
9
 * TTLがリクエストによりずれる問題はAPCuの仕様なので対応しない
10
 * @author Ryuichi Tanaka
11
 * @since 2015/07/05
12
 * @version 0.7
13
 */
14
class Apcu implements ICache
15
{
16
    use injector;
17
18
    /**
19
     * @var bool キャッシュ使用可能フラグ
20
     */
21
    private $isAvairable;
22
23
    /**
24
     * @var string キャッシュ接頭辞
25
     */
26
    private $cachePrefix;
27
28
    /**
29
     * constructor
30
     */
31
    public function __construct(Container $container)
32
    {
33
        $this->isAvairable = extension_loaded('apcu');
34
        $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...
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function add($key, $value, $ttl = 0, $overrite = false): bool
41
    {
42
        if (!$this->isAvailableCacheLibrary()) {
43
            return false;
44
        }
45
        $key = $this->cachePrefix . $key;
46
47
        $result = $overrite ? apcu_store($key, $value, $ttl) : apcu_add($key, $value, $ttl);
48
        $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\Apcu>. 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...
49
50
        return $result;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function get($key)
57
    {
58
        if (!$this->isAvailableCacheLibrary()) {
59
            return null;
60
        }
61
        $key = $this->cachePrefix . $key;
62
        $value = apcu_fetch($key, $isSuccess);
0 ignored issues
show
Bug introduced by
The variable $isSuccess does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
64
        if ($isSuccess) {
65
            $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\Apcu>. 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...
66
        } else {
67
            $this->logger->warn("Failed to read cache: " . $key);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Apcu>. 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...
68
            $value = null;
69
        }
70
71
        return $value;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function delete($key): bool
78
    {
79
        if (!$this->isAvailableCacheLibrary()) {
80
            return false;
81
        }
82
        $key = $this->cachePrefix . $key;
83
84
        return apcu_delete($key);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function clear(): bool
91
    {
92
        if (!$this->isAvailableCacheLibrary()) {
93
            return false;
94
        }
95
96
        if (class_exists('\APCUIterator')) {
97
            return apcu_delete(new \APCUIterator('/^' . $this->cachePrefix . '/', APC_ITER_KEY));
98
        } else {
99
            return apcu_clear_cache();
100
        }
101
    }
102
103
    /**
104
     * キャッシュライブラリが使用可能か検査する
105
     * @return bool 使用可能でtrue
106
     */
107
    private function isAvailableCacheLibrary(): bool
108
    {
109
        if ($this->isAvairable) {
110
            return true;
111
        }
112
113
        $this->logger->warn("APCu cache library is unavailable.");
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<WebStream\Cache\Driver\Apcu>. 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...
114
115
        return false;
116
    }
117
}
118