Completed
Push — master ( ac573b...517517 )
by Timo
02:43
created

ConfigHelper::_getOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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 $config
20
     * @param RequestLimitRuleset $rules
21
     * @return RequestLimitRuleset
22
     * @throws \Exception
23
     */
24 6
    public static function getRequestLimitRuleset() : RequestLimitRuleset
25
    {
26 6
        $config = Config::get('laravel-guzzle-throttle');
27
28 6
        if (!isset($config['cache']['driver']))
29
        {
30 1
            throw new DriverNotSetException();
31
        }
32
33 5
        return new RequestLimitRuleset(
34 5
            $config['rules'],
35 5
            $config['cache']['strategy'] ?? 'no-cache',
36 5
            'laravel',
37 5
            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

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