Test Failed
Push — master ( 9f82b3...3c8bc2 )
by Alexey
07:31
created

CacheServiceProvider::boot()   C

Complexity

Conditions 13
Paths 23

Size

Total Lines 48
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
cc 13
eloc 42
nc 23
nop 0
ccs 0
cts 48
cp 0
crap 182
rs 5.0877

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types = 1);
2
3
namespace Venta\Framework\ServiceProvider;
4
5
use Cache\Adapter\PHPArray\ArrayCachePool;
6
use InvalidArgumentException;
7
use Psr\Cache\CacheItemPoolInterface;
8
use RuntimeException;
9
use Throwable;
10
use Venta\Cache\Cache;
11
use Venta\Contracts\Cache\Cache as CacheContract;
12
use Venta\Contracts\Config\Config;
13
use Venta\Contracts\Debug\ErrorHandler;
14
use Venta\Contracts\Kernel\Kernel;
15
use Venta\ServiceProvider\AbstractServiceProvider;
16
17
/**
18
 * Class CacheServiceProvider
19
 *
20
 * @package Venta\Framework\ServiceProvider
21
 */
22
class CacheServiceProvider extends AbstractServiceProvider
23
{
24
    /**
25
     * @param $name
26
     * @param $arguments
27
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
28
     * @throws InvalidArgumentException
29
     */
30
    public function __call($name, $arguments)
31
    {
32
        throw new InvalidArgumentException(
33
            sprintf('Unknown cache driver "%s" specified.', substr($name, 0, strlen($name) - 16))
34
        );
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function boot()
41
    {
42
        $this->container()->bindClass(CacheContract::class, Cache::class);
43
        $this->tryLoadConfig();
44
        $config = $this->container()->get(Config::class);
45
        try {
46
            if (!isset($config->cache->driver)) {
47
                throw new RuntimeException('Undefined cache driver.');
48
            }
49
            switch ($config->cache->driver) {
50
                case 'array':
51
                case 'memory':
52
                    $this->container()->bindClass(CacheItemPoolInterface::class, ArrayCachePool::class, true);
53
                    break;
54
                case 'void':
55
                case 'null':
56
                    $this->container()
57
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Void\VoidCachePool', true);
58
                    break;
59
                case 'file':
60
                case 'files':
61
                case 'filesystem':
62
                    $this->container()
63
                         ->bindFactory(CacheItemPoolInterface::class, $this->filesystemCachePoolFactory(), true);
64
                    break;
65
                case 'redis':
66
                    $this->container()
67
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Redis\RedisCachePool', true);
68
                    break;
69
                case 'predis':
70
                    $this->container()
71
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Predis\PredisCachePool', true);
72
                    break;
73
                case 'memcached':
74
                    $this->container()
75
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Memcached\MemcachedCachePool', true);
76
                    break;
77
                default:
78
                    $this->container()->bindClass(
79
                        CacheItemPoolInterface::class,
80
                        ([$this, $config->cache->driver . 'CachePoolFactory'])(),
81
                        true
82
                    );
83
            }
84
        } catch (Throwable $e) {
85
            $this->container()->get(ErrorHandler::class)->handleThrowable($e);
86
        }
87
    }
88
89
    /**
90
     * @return callable
91
     */
92
    protected function filesystemCachePoolFactory(): callable
93
    {
94
        return function () {
95
            $adapter = new \League\Flysystem\Adapter\Local($this->container()->get(Kernel::class)->rootPath());
96
97
            return new \Cache\Adapter\Filesystem\FilesystemCachePool(
98
                new \League\Flysystem\Filesystem($adapter),
99
                'storage/cache'
100
            );
101
        };
102
    }
103
104
    /**
105
     * Tries to load cache config from /src/config folder.
106
     *
107
     * @return void
108
     */
109
    private function tryLoadConfig()
110
    {
111
        try {
112
            $this->loadConfigFromFiles($this->container()->get(Kernel::class)->rootPath() . '/config/cache.php');
113
        } catch (Throwable $e) {
114
            $this->container()->get(ErrorHandler::class)->handleThrowable($e);
115
        }
116
    }
117
118
}