Completed
Push — feature/0.7.0 ( 385c73...df772f )
by Ryuichi
03:38
created

CacheUtils::getCacheDriver()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 35
Code Lines 28

Duplication

Lines 17
Ratio 48.57 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
c 0
b 0
f 0
nc 7
nop 1
dl 17
loc 35
rs 6.7272
1
<?php
2
namespace WebStream\Module\Utility;
3
4
use WebStream\Module\Container;
5
use WebStream\Cache\Driver\ICache;
6
use WebStream\Cache\Driver\CacheDriverFactory;
7
8
/**
9
 * CacheUtils
10
 * キャッシュに関するUtility
11
 * @author Ryuichi Tanaka
12
 * @since 2016/07/17
13
 * @version 0.7
14
 */
15
trait CacheUtils
16
{
17
    use ApplicationUtils;
18
19
    /**
20
     * キャッシュドライバを返却する
21
     * @param string $cacheName キャッシュドライバ名
22
     * @return ICache キャッシュドライバ
23
     */
24
    public function getCacheDriver(string $cacheName): ICache
25
    {
26
        $driver = null;
27
        $factory = new CacheDriverFactory();
28
29
        switch ($cacheName) {
30
            case "apcu":
31
                $driver = $factory->create("WebStream\Cache\Driver\Apcu");
32
                break;
33 View Code Duplication
            case "memcached":
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...
34
                $cacheConfig = \Spyc::YAMLLoad($this->getApplicationRoot() . '/config/cache.yml');
35
                if (array_key_exists('memcached', $cacheConfig)) {
36
                    $config = new Container(false);
37
                    $config->servers = [$cacheConfig['host'], $cacheConfig['port']];
0 ignored issues
show
Documentation introduced by
The property servers 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...
38
                    $driver = $factory->create("WebStream\Cache\Driver\Memcached", $config);
39
                }
40
                break;
41 View Code Duplication
            case "redis":
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...
42
                $cacheConfig = \Spyc::YAMLLoad($this->getApplicationRoot() . '/config/cache.yml');
43
                if (array_key_exists('redis', $cacheConfig)) {
44
                    $config = new Container(false);
45
                    $config->host = $cacheConfig['host'];
0 ignored issues
show
Documentation introduced by
The property host 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...
46
                    $config->port = $cacheConfig['port'];
0 ignored issues
show
Documentation introduced by
The property port 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...
47
                    $driver = $factory->create("WebStream\Cache\Driver\Redis", $config);
48
                }
49
                break;
50
            case "temporaryFile":
51
                $config = new Container();
52
                $config->cacheDir = "/tmp";
0 ignored issues
show
Documentation introduced by
The property cacheDir 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...
53
                $driver = $factory->create("WebStream\Cache\Driver\TemporaryFile", $config);
54
                break;
55
        }
56
57
        return $driver;
58
    }
59
}
60