Completed
Push — master ( befab8...0495c3 )
by Timo
02:25
created

CacheConfigHelper::getCacheManager()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers;
4
5
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Drivers\LaravelDriver;
6
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException;
7
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException;
8
use Illuminate\Cache\CacheManager;
9
use Illuminate\Config\Repository;
10
use Illuminate\Container\Container;
11
use Illuminate\Filesystem\Filesystem;
12
13
14
/**
15
 * Class CacheConfigHelper
16
 * @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers
17
 */
18
class CacheConfigHelper
19
{
20
21
    /**
22
     * @param Repository|null $config
23
     * @return CacheManager
24
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException
25
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException
26
     */
27 4
    public static function getCacheManager(Repository $config = null) : CacheManager
28
    {
29 4
        if ($config === null || ($cacheConfig = $config->get('cache')) === null)
30
        {
31 1
            throw new LaravelCacheConfigNotSetException();
32
        }
33
34 3
        return new CacheManager(self::getContainer(new Repository($cacheConfig)));
35
    }
36
37
    /**
38
     * @param Repository $config
39
     * @return Container
40
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException
41
     */
42 4
    public static function getContainer(Repository $config) : Container
43
    {
44 4
        $driver = self::getDriver($config);
45
46 4
        $container = new Container();
47
48 4
        if ($driver === 'file')
49
        {
50 4
            $container['files'] = new Filesystem();
51
        }
52
53 4
        $container['config'] = LaravelDriver::getDriverConfig($driver, $config['options'] ?? []);
54
55 4
        return $container;
56
    }
57
58
    /**
59
     * @param Repository $config
60
     * @return string
61
     * @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheDriverNotSetException
62
     */
63 6
    public static function getDriver(Repository $config) : string
64
    {
65 6
        $driver = $config->get('driver');
66 6
        if ($driver === null)
67
        {
68 1
            throw new LaravelCacheDriverNotSetException();
69
        }
70
71 5
        return $driver;
72
    }
73
}