CacheConfigHelper::getContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 4
c 2
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers;
4
5
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers\FileDriver;
6
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers\LaravelDriver;
7
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers\MemcachedDriver;
8
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers\RedisDriver;
9
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException;
10
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\UnknownLaravelDriverException;
11
use Illuminate\Cache\CacheManager;
12
use Illuminate\Config\Repository;
13
use Illuminate\Container\Container;
14
15
class CacheConfigHelper
16
{
17
    /** @var array */
18
    private const DRIVERS = [
19
        'file'      => FileDriver::class,
20
        'redis'     => RedisDriver::class,
21
        'memcached' => MemcachedDriver::class
22
    ];
23
24 14
    public static function getCacheManager(Repository $config): CacheManager
25
    {
26 14
        return new CacheManager(self::getContainer($config));
0 ignored issues
show
Bug introduced by
self::getContainer($config) of type Illuminate\Container\Container is incompatible with the type Illuminate\Contracts\Foundation\Application expected by parameter $app of Illuminate\Cache\CacheManager::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        return new CacheManager(/** @scrutinizer ignore-type */ self::getContainer($config));
Loading history...
27
    }
28
29 15
    public static function getContainer(Repository $config): Container
30
    {
31 15
        $driverName = self::getDriver($config);
32 15
        $driverClass = self::_getDriverClass($driverName);
33
34
        /** @var LaravelDriver $driverClass */
35 14
        $driverClass = new $driverClass($driverName, $config['options'] ?? []);
36
37 14
        return $driverClass->getContainer();
38
    }
39
40 17
    public static function getDriver(Repository $config): string
41
    {
42 17
        $driver = $config->get('driver');
43 17
        if ($driver === null) {
44 1
            throw new LaravelCacheDriverNotSetException();
45
        }
46
47 16
        return $driver;
48
    }
49
50 15
    private static function _getDriverClass(string $driverName): string
51
    {
52 15
        if (!isset(self::DRIVERS[$driverName])) {
53 1
            throw new UnknownLaravelDriverException($driverName, self::DRIVERS);
54
        }
55
56 14
        return self::DRIVERS[$driverName];
57
    }
58
}
59