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
Pull Request — master (#2)
by Hilari
02:23
created

CacheServiceProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.87%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 116
ccs 37
cts 39
cp 0.9487
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A register() 0 11 1
A addBackend() 0 16 4
A getParameter() 0 4 2
A build() 0 16 4
A buildRedis() 0 17 3
1
<?php
2
3
namespace Cmp\Cache\Factory\Pimple;
4
5
use Cmp\Cache\Backend\RedisCache;
6
use Cmp\Cache\Cache;
7
use Cmp\Cache\Factory\CacheBuilder;
8
use Pimple\Container;
9
use Pimple\ServiceProviderInterface;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\LogLevel;
12
use Redis;
13
14
/**
15
 * Class CacheServiceProvider
16
 *
17
 * @package Cmp\Cache\Factory\Pimple
18
 */
19
class CacheServiceProvider implements ServiceProviderInterface
20
{
21
    /**
22
     * @var CacheBuilder
23
     */
24
    private $builder;
25
26
    /**
27
     * PimpleCacheProvider constructor.
28
     *
29
     * @param CacheBuilder|null $builder
30
     */
31 1
    public function __construct(CacheBuilder $builder = null)
32
    {
33 1
        $this->builder = $builder ?: new CacheBuilder();
34 1
    }
35
36
    /**
37
     * Registers cache services on the given container.
38
     *
39
     * @param Container $pimple A container instance
40
     */
41 1
    public function register(Container $pimple)
42
    {
43 1
        $pimple['cache.backends']   = ['array' => ['backend' => 'array']];
44 1
        $pimple['cache.exceptions'] = true;
45 1
        $pimple['cache.logger']     = null;
46 1
        $pimple['cache.log_level']  = LogLevel::ALERT;
47
48 1
        $pimple['cache'] = function () use ($pimple) {
49 1
            return $this->build($pimple);
50
        };
51 1
    }
52
53
    /**
54
     * @param Container $pimple
55
     *
56
     * @return Cache
57
     */
58 1
    private function build(Container $pimple)
59
    {
60 1
        if (!$pimple['cache.exceptions']) {
61 1
            $this->builder->withoutExceptions();
62
        }
63
64 1
        if ($pimple['cache.logger'] instanceof LoggerInterface) {
65
            $this->builder->withLogging($pimple['cache.logger'], $pimple['cache.log_level']);
66
        }
67
68 1
        foreach ($pimple['cache.backends'] as $backend) {
69 1
            $this->addBackend($pimple, $backend['backend'], $backend);
70
        }
71
72 1
        return $this->builder->build();
73
    }
74
75
    /**
76
     * @param Container $pimple
77
     * @param string    $backend
78
     * @param array     $options
79
     *
80
     * @return $this|CacheServiceProvider
81
     */
82 1
    private function addBackend(Container $pimple, $backend, array $options)
83
    {
84 1
        if ($backend == 'array') {
85 1
            return $this->builder->withArrayCache();
86
        }
87
88 1
        if ($backend == 'redis') {
89 1
            return $this->buildRedis($pimple, $options);
90
        }
91
92 1
        if ($backend instanceof Cache) {
93 1
            return $this->builder->withCache($backend);
94
        }
95
96
        return $this->builder->withCache($pimple[$backend]);
97
    }
98
99
    /**
100
     * @param Container $pimple
101
     * @param array     $options
102
     *
103
     * @return $this
104
     */
105 1
    private function buildRedis(Container $pimple, array $options)
106
    {
107 1
        if (!isset($options['connection'])) {
108 1
            return $this->builder->withRedisCacheFromParams(
109 1
                $this->getParameter($options, 'host', RedisCache::DEFAULT_HOST),
110 1
                $this->getParameter($options, 'port', RedisCache::DEFAULT_HOST),
111 1
                $this->getParameter($options, 'db', RedisCache::DEFAULT_DB),
112 1
                $this->getParameter($options, 'timeout', RedisCache::DEFAULT_TIMEOUT)
113
            );
114
        }
115
116 1
        if ($options['connection'] instanceof Redis) {
117 1
            return $this->builder->withRedis($options['connection']);
118
        }
119
120 1
        return $this->builder->withRedis($pimple[$options['connection']]);
121
    }
122
123
    /**
124
     * @param array  $options
125
     * @param string $key
126
     * @param mixed  $default
127
     *
128
     * @return mixed
129
     */
130 1
    private function getParameter(array $options, $key, $default)
131
    {
132 1
        return isset($options[$key]) ? $options[$key] : $default;
133
    }
134
}
135