Completed
Push — master ( cede30...95d1c4 )
by Timo
02:21
created

ConfigHelper::getConfigName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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 $config
20
     * @param RequestLimitRuleset $rules
21
     * @return Client
22
     * @throws \Exception
23
     */
24 4
    public static function getRequestLimitRuleset() : RequestLimitRuleset
25
    {
26 4
        $config = Config::get('laravel-guzzle-throttle');
27
28 4
        if (!isset($config['cache']['driver']))
29
        {
30 1
            throw new DriverNotSetException();
31
        }
32
33 3
        return new RequestLimitRuleset(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new hamburgsclean...che']['ttl'] ?? null))) returns the type hamburgscleanest\GuzzleA...tle\RequestLimitRuleset which is incompatible with the documented return type hamburgscleanest\Laravel...Throttle\Helpers\Client.
Loading history...
34 3
            $config['rules'],
35 3
            $config['cache']['strategy'] ?? 'no-cache',
36 3
            'laravel',
37 3
            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 3
    private static function _getMiddlewareConfig(string $driverName, int $ttl) : array
47
    {
48 3
        $driverConfig = self::getConfigForDriver($driverName);
49 3
        $driver = $driverConfig['driver'] ?? 'file';
50 3
        unset($driverConfig['driver']);
51
52
        return [
53
            'cache' => [
54 3
                'driver'  => $driver,
55 3
                'options' => $driverConfig,
56 3
                'ttl'     => $ttl
57
            ]
58
        ];
59
    }
60
61
    /**
62
     * @param string $driverName
63
     * @return array
64
     */
65 3
    public static function getConfigForDriver(string $driverName) : array
66
    {
67 3
        return Config::get('cache.stores.' . self::getConfigName($driverName));
68
    }
69
70
    /**
71
     * @param string $driverName
72
     * @return string
73
     */
74 3
    public static function getConfigName(string $driverName) : string
75
    {
76 3
        return $driverName === 'default' ? Config::get('cache.default') : $driverName;
77
    }
78
}