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