GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bb4133...5e3587 )
by Hilari
02:54
created

PimpleCacheProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 76
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 15 2
A getCache() 0 12 4
A getRedis() 0 13 2
A getParameter() 0 4 2
1
<?php
2
3
namespace Cmp\Cache\Infrastructure\Provider;
4
5
use Closure;
6
use Cmp\Cache\Application\CacheFactory;
7
use Cmp\Cache\Infrastructure\RedisCache;
8
use InvalidArgumentException;
9
use Pimple\Container;
10
use Pimple\ServiceProviderInterface;
11
use Redis;
12
13
/**
14
 * Class PimpleCacheProvider
15
 *
16
 * @package Cmp\Cache\Infrastructure\Provider
17
 */
18
class PimpleCacheProvider implements ServiceProviderInterface
19
{
20
    /**
21
     * Registers cache services on the given container.
22
     *
23
     * @param Container $pimple A container instance
24
     */
25
    public function register(Container $pimple)
26
    {
27
        $pimple['cache.backend'] = 'array';
28
        $pimple['cache.debug']   = false;
29
30
        $pimple['cache'] = function () use ($pimple) {
31
            $cache = $this->getCache($pimple['cache.backend']);
32
33
            if ($pimple['cache.debug']) {
34
                $cache = CacheFactory::decorateForTesting($cache);
35
            }
36
37
            return $cache;
38
        };
39
    }
40
41
    /**
42
     * Gets a cache object based on the backend requested
43
     *
44
     * @param $backend
45
     *
46
     * @return \Cmp\Cache\Domain\Cache
47
     */
48
    private function getCache($backend)
49
    {
50
        if (is_array($backend) && isset($backend['redis'])) {
51
            return $this->getRedis($backend['redis']);
52
        }
53
54
        if ($backend == 'array') {
55
            return CacheFactory::arrayCache();
56
        }
57
58
        throw new InvalidArgumentException("Invalid cache backend");
59
    }
60
61
    /**
62
     * Return a factory closure to build
63
     *
64
     * @param array|Redis $redis
65
     *
66
     * @return \Cmp\Cache\Domain\Cache
67
     */
68
    private function getRedis($redis)
69
    {
70
        if ($redis instanceof Redis) {
1 ignored issue
show
Bug introduced by
The class Redis does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
71
            return CacheFactory::redisCache($redis);
72
        }
73
74
        $host    = $this->getParameter($redis, 'host', RedisCache::DEFAULT_HOST);
75
        $port    = $this->getParameter($redis, 'port', RedisCache::DEFAULT_PORT);
76
        $db      = $this->getParameter($redis, 'db', RedisCache::DEFAULT_DB);
77
        $timeout = $this->getParameter($redis, 'timeout', RedisCache::DEFAULT_TIMEOUT);
78
79
        return CacheFactory::redisFromParams($host, $port, $db, $timeout);
80
    }
81
82
    /**
83
     * @param array  $options
84
     * @param string $key
85
     * @param mixed  $default
86
     *
87
     * @return mixed
88
     */
89
    private function getParameter(array $options, $key, $default)
90
    {
91
        return isset($options[$key]) ? $options[$key] : $default;
92
    }
93
}
94