Passed
Push — master ( ac3ce9...4824e6 )
by Alexey
04:00
created

CacheServiceProvider::filesystemCachePoolFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Framework\ServiceProvider;
4
5
use Cache\Adapter\Filesystem\FilesystemCachePool;
6
use Cache\Adapter\PHPArray\ArrayCachePool;
7
use InvalidArgumentException;
8
use League\Flysystem\Filesystem;
9
use Psr\Cache\CacheItemPoolInterface;
10
use RuntimeException;
11
use Venta\Cache\Cache;
12
use Venta\Contracts\Cache\Cache as CacheContract;
13
use Venta\Contracts\Config\Config;
14
use Venta\Contracts\Container\MutableContainer;
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 bind(MutableContainer $container)
41
    {
42
        $container->bind(CacheContract::class, Cache::class);
43
        /** @var Config $config */
44
        $config = $container->get(Config::class);
45
46
        $cacheDriver = $config->get('cache.driver');
47
        if ($cacheDriver === null) {
48
            throw new RuntimeException('Undefined cache driver.');
49
        }
50
        switch ($cacheDriver) {
51
            case 'array':
52
            case 'memory':
53
                $container->bind(CacheItemPoolInterface::class, ArrayCachePool::class);
54
                break;
55
            case 'void':
56
            case 'null':
57
                $container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Void\VoidCachePool');
58
                break;
59
            case 'file':
60
            case 'files':
61
            case 'filesystem':
62
                $container
63
                    ->factory(CacheItemPoolInterface::class, $this->filesystemCachePoolFactory(), true);
64
                break;
65
            case 'redis':
66
                $container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Redis\RedisCachePool');
67
                break;
68
            case 'predis':
69
                $container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Predis\PredisCachePool');
70
                break;
71
            case 'memcached':
72
                $container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Memcached\MemcachedCachePool');
73
                break;
74
            default:
75
                $container->bind(CacheItemPoolInterface::class, ([$this, $cacheDriver . 'CachePoolFactory'])());
76
        }
77
    }
78
79
    /**
80
     * @return callable
81
     */
82
    protected function filesystemCachePoolFactory(): callable
83
    {
84
        return function (Filesystem $flysystem) {
85
            return new FilesystemCachePool($flysystem, 'storage/cache');
86
        };
87
    }
88
}