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
Branch pimple-service (41dc28)
by Hilari
02:55
created

CacheServiceProvider::addBackend()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 4
nop 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
    public function __construct(CacheBuilder $builder = null)
32
    {
33
        $this->builder = $builder ?: new CacheBuilder();
34
    }
35
36
    /**
37
     * Registers cache services on the given container.
38
     *
39
     * @param Container $pimple A container instance
40
     */
41
    public function register(Container $pimple)
42
    {
43
        $pimple['cache.backends']   = ['array' => ['backend' => 'array']];
44
        $pimple['cache.exceptions'] = true;
45
        $pimple['cache.logger']     = null;
46
        $pimple['cache.log_level']  = LogLevel::ALERT;
47
48
        $pimple['cache'] = function () use ($pimple) {
49
            return $this->build($pimple);
50
        };
51
    }
52
53
    /**
54
     * @param Container $pimple
55
     *
56
     * @return Cache
57
     */
58
    private function build(Container $pimple)
59
    {
60
        if (!$pimple['cache.exceptions']) {
61
            $this->builder->withoutExceptions();
62
        }
63
64
        if ($pimple['cache.logger'] instanceof LoggerInterface) {
65
            $this->builder->withLogging($pimple['cache.logger'], $pimple['cache.log_level']);
66
        }
67
68
        foreach ($pimple['cache.backends'] as $backend) {
69
            $this->addBackend($pimple, $backend['backend'], $backend);
70
        }
71
72
        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
    private function addBackend(Container $pimple, $backend, array $options)
83
    {
84
        if ($backend == 'array') {
85
            return $this->builder->withArrayCache();
86
        }
87
88
        if ($backend == 'redis') {
89
            return $this->buildRedis($pimple, $options);
90
        }
91
92
        if ($backend instanceof Cache) {
93
            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
    private function buildRedis(Container $pimple, array $options)
106
    {
107
        if (!isset($options['connection'])) {
108
            return $this->builder->withRedisCacheFromParams(
109
                $this->getParameter($options, 'host', RedisCache::DEFAULT_HOST),
110
                $this->getParameter($options, 'port', RedisCache::DEFAULT_HOST),
111
                $this->getParameter($options, 'db', RedisCache::DEFAULT_DB),
112
                $this->getParameter($options, 'timeout', RedisCache::DEFAULT_TIMEOUT)
113
            );
114
        }
115
116
        if ($options['connection'] instanceof Redis) {
117
            return $this->builder->withRedis($options['connection']);
118
        }
119
120
        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
    private function getParameter(array $options, $key, $default)
131
    {
132
        return isset($options[$key]) ? $options[$key] : $default;
133
    }
134
}
135