CacheServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 44
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A register() 0 29 3
1
<?php
2
namespace Germania\CacheServiceProvider;
3
4
use Pimple\Container;
5
use Pimple\ServiceProviderInterface;
6
7
use phpFastCache\CacheManager;
8
9
class CacheServiceProvider implements ServiceProviderInterface
10
{
11
    public $cache_lifetime;
12
    public $cache_dir;
13
14 10
    public function __construct($cache_dir = null, $cache_lifetime = 3600)
15
    {
16 10
        $this->cache_dir = $cache_dir ?: sys_get_temp_dir();
17 10
        $this->cache_lifetime = (int) $cache_lifetime;
18 10
    }
19
20
    /**
21
     * @implements ServiceProviderInterface
22
     */
23 8
    public function register(Container $dic)
24
    {
25
26 2
        $dic['Cache.lifetime'] = function($dic) {
0 ignored issues
show
Unused Code introduced by
The parameter $dic 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...
27 10
            return $this->cache_lifetime;
28
        };
29
30
31 2
        $dic['Cache.directory'] = function($dic) {
0 ignored issues
show
Unused Code introduced by
The parameter $dic 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...
32 10
            return $this->cache_dir;
33
        };
34
35
36 4
        $dic['Cache.ItemPool'] = function($dic) {
37 10
            $cache_dir = $dic['Cache.directory'];
38
39 10
            if (!$cache_dir) {
40
                throw new \Exception("Cache directory not available: " . $cache_dir);
41
            }
42
43 10
            if (!is_writable( $cache_dir)) {
44
                throw new \Exception("Cache directory not writeable: $cache_dir");
45
            }
46
47 10
            return CacheManager::getInstance('files', [ "path" => $cache_dir ]);
48
        };
49
50
51 10
    }
52
}
53
54