Completed
Push — master ( 3f74f0...a713f8 )
by Timo
04:12
created

ConfigHelper::getMiddlewareConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\LaravelGuzzleThrottle\Helpers;
4
5
use hamburgscleanest\GuzzleAdvancedThrottle\RequestLimitRuleset;
6
use hamburgscleanest\LaravelGuzzleThrottle\Exceptions\DriverNotSetException;
7
use Illuminate\Config\Repository;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\ServiceProvider;
10
11
/**
12
 * Class ConfigHelper
13
 * @package hamburgscleanest\LaravelGuzzleThrottle
14
 */
15
class ConfigHelper extends ServiceProvider
16
{
17
18
    /**
19
     * @param array|null $config
20
     * @return RequestLimitRuleset
21
     * @throws \hamburgscleanest\LaravelGuzzleThrottle\Exceptions\DriverNotSetException
22
     */
23 7
    public static function getRequestLimitRuleset(array $config = null) : RequestLimitRuleset
24
    {
25 7
        if ($config === null)
26
        {
27 6
            $config = Config::get('laravel-guzzle-throttle');
28
        }
29
30 7
        if (!isset($config['cache']['driver']))
31
        {
32 1
            throw new DriverNotSetException();
33
        }
34
35 6
        return new RequestLimitRuleset(
36 6
            $config['rules'],
37 6
            $config['cache']['strategy'] ?? 'no-cache',
38 6
            'laravel',
39 6
            new Repository(self::getMiddlewareConfig($config['cache']['driver'], $config['cache']['ttl'] ?? null))
0 ignored issues
show
Bug introduced by
It seems like $config['cache']['ttl'] ?? null can also be of type null; however, parameter $ttl of hamburgscleanest\Laravel...::getMiddlewareConfig() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

39
            new Repository(self::getMiddlewareConfig($config['cache']['driver'], /** @scrutinizer ignore-type */ $config['cache']['ttl'] ?? null))
Loading history...
40
        );
41
    }
42
43
    /**
44
     * @param string $driverName
45
     * @param int $ttl
46
     * @return array
47
     */
48 6
    public static function getMiddlewareConfig(string $driverName, int $ttl) : array
49
    {
50 6
        $driverConfig = self::getConfigForDriver($driverName);
51 6
        $driver = $driverConfig['driver'] ?? 'file';
52 6
        unset($driverConfig['driver']);
53
54
        return [
55
            'cache' => [
56 6
                'driver'  => $driver,
57 6
                'options' => self::_getOptions($driverName, $driverConfig),
58 6
                'ttl'     => $ttl
59
            ]
60
        ];
61
    }
62
63
    /**
64
     * @param string $driverName
65
     * @return array
66
     */
67 6
    public static function getConfigForDriver(string $driverName) : array
68
    {
69 6
        return Config::get('cache.stores.' . self::getConfigName($driverName));
70
    }
71
72
    /**
73
     * @param string $driverName
74
     * @return string
75
     */
76 6
    public static function getConfigName(string $driverName) : string
77
    {
78 6
        return $driverName === 'default' ? Config::get('cache.default') : $driverName;
79
    }
80
81
    /**
82
     * Needs to be refactored :)
83
     *
84
     * @param string $driverName
85
     * @param array $config
86
     * @return array
87
     */
88 6
    private static function _getOptions(string $driverName, array $config) : array
89
    {
90 6
        if ($driverName !== 'redis')
91
        {
92 4
            return $config;
93
        }
94
95 2
        return ['database' => Config::get('database.redis.' . ($config['connection'] ?? 'default'))];
96
    }
97
}
98