Passed
Push — master ( 754eba...577a19 )
by Vsevolods
03:09
created

CacheServiceProvider::tryLoadConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
ccs 0
cts 8
cp 0
crap 6
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 Throwable;
12
use Venta\Cache\Cache;
13
use Venta\Contracts\Cache\Cache as CacheContract;
14
use Venta\Contracts\Config\Config;
15
use Venta\Contracts\Debug\ErrorHandler;
16
use Venta\ServiceProvider\AbstractServiceProvider;
17
18
/**
19
 * Class CacheServiceProvider
20
 *
21
 * @package Venta\Framework\ServiceProvider
22
 */
23
class CacheServiceProvider extends AbstractServiceProvider
24
{
25
    /**
26
     * @param $name
27
     * @param $arguments
28
     * @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...
29
     * @throws InvalidArgumentException
30
     */
31
    public function __call($name, $arguments)
32
    {
33
        throw new InvalidArgumentException(
34
            sprintf('Unknown cache driver "%s" specified.', substr($name, 0, strlen($name) - 16))
35
        );
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function boot()
42
    {
43
        $this->container()->bindClass(CacheContract::class, Cache::class);
44
        /** @var Config $config */
45
        $config = $this->container()->get(Config::class);
46
        try {
47
            $cacheDriver = $config->get('cache.driver');
48
            if ($cacheDriver === null) {
49
                throw new RuntimeException('Undefined cache driver.');
50
            }
51
            switch ($cacheDriver) {
52
                case 'array':
53
                case 'memory':
54
                    $this->container()->bindClass(CacheItemPoolInterface::class, ArrayCachePool::class, true);
55
                    break;
56
                case 'void':
57
                case 'null':
58
                    $this->container()
59
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Void\VoidCachePool', true);
60
                    break;
61
                case 'file':
62
                case 'files':
63
                case 'filesystem':
64
                    $this->container()
65
                         ->bindFactory(CacheItemPoolInterface::class, $this->filesystemCachePoolFactory(), true);
66
                    break;
67
                case 'redis':
68
                    $this->container()
69
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Redis\RedisCachePool', true);
70
                    break;
71
                case 'predis':
72
                    $this->container()
73
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Predis\PredisCachePool', true);
74
                    break;
75
                case 'memcached':
76
                    $this->container()
77
                         ->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Memcached\MemcachedCachePool', true);
78
                    break;
79
                default:
80
                    $this->container()->bindClass(
81
                        CacheItemPoolInterface::class,
82
                        ([$this, $config->cache->driver . 'CachePoolFactory'])(),
0 ignored issues
show
Bug introduced by
Accessing cache on the interface Venta\Contracts\Config\Config suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83
                        true
84
                    );
85
            }
86
        } catch (Throwable $e) {
87
            $this->container()->get(ErrorHandler::class)->handleThrowable($e);
88
        }
89
    }
90
91
    /**
92
     * @return callable
93
     */
94
    protected function filesystemCachePoolFactory(): callable
95
    {
96
        return function (Filesystem $flysystem) {
97
            return new FilesystemCachePool($flysystem, 'storage/cache');
98
        };
99
    }
100
}