Completed
Push — 4.0 ( 2ca4e1...0c3d7c )
by Marco
15:24
created

DispatcherCache   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 25
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 133
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D init() 0 33 9
A create() 0 7 1
D getHandler() 0 44 9
B getAlgorithm() 0 30 6
1
<?php namespace Comodojo\Dispatcher\Cache;
2
3
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
4
use \comodojo\Dispatcher\Components\Configuration;
5
use \Comodojo\Cache\CacheInterface\CacheInterface;
6
use \Comodojo\Cache\CacheManager;
7
use \Comodojo\Cache\FileCache;
8
use \Psr\Log\LoggerInterface;
9
10
/**
11
 * @package     Comodojo Dispatcher
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @author      Marco Castiello <[email protected]>
14
 * @license     GPL-3.0+
15
 *
16
 * LICENSE:
17
 *
18
 * This program is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License as
20
 * published by the Free Software Foundation, either version 3 of the
21
 * License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30
 */
31
32
class DispatcherCache extends DispatcherClassModel{
33
34
    public function init() {
35
36
        $cache = $this->configuration()->get('cache');
37
38
        if ( empty($cache) ) {
39
40
            $manager = new CacheManager(self::getAlgorithm(), $this->logger);
41
42
        } else {
43
44
            $enabled = ( empty($cache['enabled']) || $cache['enabled'] === true ) ? true : false;
45
46
            $algorithm = self::getAlgorithm( empty($cache['algorithm']) ? null : $cache['algorithm']);
47
48
            $manager = new CacheManager($algorithm, $this->logger);
49
50
            if ( $enabled && !empty($cache['providers']) ) {
51
52
                foreach ($cache['providers'] as $provider => $parameters) {
53
54
                    $handler = $this->getHandler($provider, $parameters);
55
56
                    if ( $handler instanceof CacheInterface ) $manager->addProvider($handler);
57
58
                }
59
60
            }
61
62
        }
63
64
        return $manager;
65
66
    }
67
68
    /**
69
     * Create the Cache Manager
70
     *
71
     * @return \Comodojo\Cache\CacheManager
72
     */
73
    public static function create(Configuration $configuration, LoggerInterface $logger) {
74
75
        $cache = new DispatcherCache($configuration, $logger);
0 ignored issues
show
Compatibility introduced by
$logger of type object<Psr\Log\LoggerInterface> is not a sub-type of object<Monolog\Logger>. It seems like you assume a concrete implementation of the interface Psr\Log\LoggerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
76
77
        return $cache->init();
78
79
    }
80
81
    protected function getHandler($provider, $parameters) {
82
83
        switch ( strtolower($parameters['type']) ) {
84
85
            case 'file':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
86
87
                $base = $this->configuration->get('base-path');
88
89
                if ( empty($parameters['folder']) ||  empty($base) ) {
90
                    $this->logger->warning("Wrong cache provider, disabling $provider", $parameters);
91
                    break;
92
                }
93
94
                $target = $base.'/'.$parameters['folder'];
95
96
                $handler = new FileCache($target);
97
98
                break;
99
100
            case 'memcached':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
101
102
                if ( empty($parameters['host']) ) {
103
                    $this->logger->warning("Wrong cache provider, disabling $provider", $parameters);
104
                    break;
105
                }
106
107
                $port = empty($parameters['port']) ? 11211 : intval($parameters['port']);
108
109
                $weight = empty($parameters['weight']) ? 0 : intval($parameters['weight']);
110
111
                $persistentid = empty($parameters['persistent-id']) ? null : boolval($parameters['persistentid']);
112
113
                $handler = new MemcachedCache($host, $port, $weight, $persistentid);
0 ignored issues
show
Bug introduced by
The variable $host 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...
114
115
                break;
116
117
            default:
118
                $handler = null;
119
                break;
120
        }
121
122
        return $handler;
0 ignored issues
show
Bug introduced by
The variable $handler does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
123
124
    }
125
126
    /**
127
     * Map provided log level to level code
128
     *
129
     * @param   string    $algorithm
130
     *
131
     * @return  integer
132
     */
133
    protected static function getAlgorithm($algorithm = null) {
134
135
        switch ( strtoupper($algorithm) ) {
136
137
            case 'PICK_LAST':
138
                $selected = CacheManager::PICK_LAST;
139
                break;
140
141
            case 'PICK_RANDOM':
142
                $selected = CacheManager::PICK_RANDOM;
143
                break;
144
145
            case 'PICK_BYWEIGHT':
146
                $selected = CacheManager::PICK_BYWEIGHT;
147
                break;
148
149
            case 'PICK_ALL':
150
                $selected = CacheManager::PICK_ALL;
151
                break;
152
153
            case 'PICK_FIRST':
154
            default:
155
                $selected = CacheManager::PICK_FIRST;
156
                break;
157
158
        }
159
160
        return $selected;
161
162
    }
163
164
}
165