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)) |
|
|
|
|
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
|
|
|
|