Completed
Push — 4.0 ( 6e7e32...0ca36c )
by Marco
12:30
created

DispatcherCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace Comodojo\Dispatcher\Cache;
2
3
use \Psr\Log\LoggerInterface;
4
use \Comodojo\Cache\CacheInterface\CacheInterface;
5
use \Comodojo\Cache\CacheManager;
6
use \Comodojo\Cache\FileCache;
7
use \comodojo\Dispatcher\Components\Configuration;
8
9
/**
10
 * @package     Comodojo Dispatcher
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @author      Marco Castiello <[email protected]>
13
 * @license     GPL-3.0+
14
 *
15
 * LICENSE:
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 */
30
31
class DispatcherCache {
32
33
    private $configuration;
34
35
    private $logger;
36
37
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
38
39
        $this->configuration = $configuration;
40
41
        $this->logger = $logger;
42
43
    }
44
45
    public function init() {
46
47
        $cache = $this->configuration->get('cache');
48
49
        if ( empty($cache) ) {
50
51
            $manager = new CacheManager(self::getAlgorithm(), $this->logger);
0 ignored issues
show
Documentation introduced by
$this->logger is of type object<Psr\Log\LoggerInterface>, but the function expects a null|object<Monolog\Logger>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
53
        } else {
54
55
            $enabled = ( empty($cache['enabled']) || $cache['enabled'] === true ) ? true : false;
56
57
            $algorithm = self::getAlgorithm( empty($cache['algorithm']) ? null : $cache['algorithm']);
58
59
            $manager = new CacheManager($algorithm, $this->logger);
0 ignored issues
show
Documentation introduced by
$this->logger is of type object<Psr\Log\LoggerInterface>, but the function expects a null|object<Monolog\Logger>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61
            if ( $enabled && !empty($cache['providers']) ) {
62
63
                foreach ($cache['providers'] as $provider => $parameters) {
64
65
                    $handler = $this->getHandler($provider, $parameters);
66
67
                    if ( $handler instanceof CacheInterface ) $manager->addProvider($handler);
68
69
                }
70
71
            }
72
73
        }
74
75
        return $manager;
76
77
    }
78
79
    /**
80
     * Create the Cache Manager
81
     *
82
     * @return \Comodojo\Cache\CacheManager
83
     */
84
    public static function create(Configuration $configuration, LoggerInterface $logger) {
85
86
        $cache = new DispatcherCache($configuration, $logger);
87
88
        return $cache->init();
89
90
    }
91
92
    protected function getHandler($provider, $parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $provider is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
94
        switch ( strtolower($parameters['type']) ) {
95
96
            case 'filecache':
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...
97
98
                $folder = empty($parameters['folder']) ? '' : $parameters['$folder'];
99
100
                $target = $this->configuration->get('base-path').'/'.$folder;
101
102
                $handler = new FileCache($target);
103
104
                break;
105
106
            default:
107
                $handler = null;
108
                break;
109
        }
110
111
        return $handler;
112
113
    }
114
115
    /**
116
     * Map provided log level to level code
117
     *
118
     * @param   string    $algorithm
119
     *
120
     * @return  integer
121
     */
122
    protected static function getAlgorithm($algorithm = null) {
123
124
        switch ( strtoupper($algorithm) ) {
125
126
            case 'PICK_LAST':
127
                $selected = CacheManager::PICK_LAST;
128
                break;
129
130
            case 'PICK_RANDOM':
131
                $selected = CacheManager::PICK_RANDOM;
132
                break;
133
134
            case 'PICK_BYWEIGHT':
135
                $selected = CacheManager::PICK_BYWEIGHT;
136
                break;
137
138
            case 'PICK_ALL':
139
                $selected = CacheManager::PICK_ALL;
140
                break;
141
142
            case 'PICK_FIRST':
143
            default:
144
                $selected = CacheManager::PICK_FIRST;
145
                break;
146
147
        }
148
149
        return $selected;
150
151
    }
152
153
}
154