RegisterCachePools   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 86
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setPools() 0 7 2
A addPool() 0 5 1
A getPools() 0 11 3
B resolvePool() 0 36 5
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/craft-psr6/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/craft-psr6
7
 */
8
9
namespace flipbox\craft\psr6\events;
10
11
use Craft;
12
use craft\helpers\Json;
13
use flipbox\spark\helpers\ObjectHelper;
14
use Stash\Interfaces\PoolInterface;
15
use yii\base\Event;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class RegisterCachePools extends Event
22
{
23
    /**
24
     * @var PoolInterface[]
25
     */
26
    protected $pools = [];
27
28
    /**
29
     * @param array $pools
30
     * @return $this
31
     */
32
    public function setPools(array $pools)
33
    {
34
        foreach ($pools as $handle => $pool) {
35
            $this->addPool($handle, $pool);
36
        }
37
        return $this;
38
    }
39
40
    /**
41
     * @param string $handle
42
     * @param $pool
43
     * @return $this
44
     */
45
    public function addPool(string $handle, $pool)
46
    {
47
        $this->pools[$handle] = $pool;
48
        return $this;
49
    }
50
51
    /**
52
     * @return PoolInterface[]
53
     */
54
    public function getPools()
55
    {
56
        $pools = [];
57
        foreach ($this->pools as $handle => $pool) {
58
            if (!$pool = $this->resolvePool($pool)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $pool is correct as $this->resolvePool($pool) (which targets flipbox\craft\psr6\event...chePools::resolvePool()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
59
                continue;
60
            }
61
            $pools[$handle] = $pool;
62
        }
63
        return $pools;
64
    }
65
66
    /**
67
     * @param $pool
68
     * @return null
69
     */
70
    protected function resolvePool($pool)
71
    {
72
        if (is_callable($pool)) {
73
            $pool = $pool();
74
        }
75
76
        if ($pool instanceof PoolInterface) {
77
            return $pool;
78
        }
79
80
        if (!$class = ObjectHelper::findClassFromConfig($pool)) {
81
            Craft::error(
82
                sprintf(
83
                    "Could find cache pool class from config: %s",
84
                    Json::encode($pool)
85
                ),
86
                'PSR-6'
87
            );
88
            return null;
89
        }
90
91
        // Make sure we have a valid class
92
        if (!is_subclass_of($class, PoolInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Stash\Interfaces\PoolInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
93
            Craft::error(
94
                sprintf(
95
                    "Cache pool class '%s' is not an instances of %s",
96
                    (string)$class,
97
                    PoolInterface::class
98
                ),
99
                'PSR-6'
100
            );
101
            return null;
102
        }
103
104
        return $class($pool);
105
    }
106
}
107