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

CacheConfigHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 9 2
A getContainer() 0 14 2
A getCacheManager() 0 8 3
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
}