Completed
Push — 4.0 ( 1bebd1...1533b9 )
by Marco
14:16
created

DispatcherCache::create()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 11
nc 4
nop 2
1
<?php namespace Comodojo\Dispatcher\Cache;
2
3
use \Monolog\Logger;
4
use \Comodojo\Cache\CacheManager;
5
use \Comodojo\Cache\FileCache;
6
7
/**
8
 *
9
 *
10
 * @package     Comodojo Framework
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
    /**
34
     * Create the Cache Manager
35
     *
36
     * @return \Comodojo\Cache\CacheManager
37
     */
38
    public static function create(Configuration $configuration, Logger $logger) {
0 ignored issues
show
Unused Code introduced by
The parameter $logger 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...
39
    
40
        $enabled = $configuration->get('dispatcher-cache-enabled');
41
        
42
        $ttl = $configuration->get('dispatcher-cache-ttl');
43
        
44
        if ( $ttl !== null && !defined('COMODOJO_CACHE_DEFAULT_TTL') ) {
45
            
46
            define('COMODOJO_CACHE_DEFAULT_TTL', $ttl);
47
            
48
        }
49
        
50
        $folder = $configuration->get('dispatcher-cache-folder');
51
        
52
        $algorithm = self::getAlgorithm( $configuration->get('dispatcher-cache-algorithm') );
53
    
54
        $manager = new CacheManager( $algorithm );
55
        
56
        if ( $enabled === true ) {
57
            
58
            $manager->addProvider( new FileCache($folder) );
59
            
60
        }
61
        
62
        return $manager;
63
        
64
    }
65
    
66
    /**
67
     * Map provided log level to level code
68
     *
69
     * @param   string    $level
0 ignored issues
show
Bug introduced by
There is no parameter named $level. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
70
     *
71
     * @return  integer
72
     */
73
    protected static function getAlgorithm($algorithm) {
74
75
        switch ( strtoupper($algorithm) ) {
76
77
            case 'PICK_LAST':
78
                $selected = CacheManager::PICK_LAST;
79
                break;
80
81
            case 'PICK_RANDOM':
82
                $selected = CacheManager::PICK_RANDOM;
83
                break;
84
85
            case 'PICK_BYWEIGHT':
86
                $selected = CacheManager::PICK_BYWEIGHT;
87
                break;
88
89
            case 'PICK_ALL':
90
                $selected = CacheManager::PICK_ALL;
91
                break;
92
93
            case 'PICK_FIRST':
94
            default:
95
                $selected = CacheManager::PICK_FIRST;
96
                break;
97
98
        }
99
100
        return $selected;
101
102
    }
103
    
104
}