ClientHelper   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 9
c 3
b 0
f 0
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _getHandlerStack() 0 8 2
A getThrottledClient() 0 6 1
1
<?php
2
3
namespace hamburgscleanest\LaravelGuzzleThrottle\Helpers;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\CurlHandler;
7
use GuzzleHttp\HandlerStack;
8
use hamburgscleanest\GuzzleAdvancedThrottle\Middleware\ThrottleMiddleware;
9
use hamburgscleanest\GuzzleAdvancedThrottle\RequestLimitRuleset;
10
use Illuminate\Support\ServiceProvider;
11
12
class ClientHelper extends ServiceProvider
13
{
14
    /** @var string */
15
    private const HANDLER_KEY = 'handler';
16
17 3
    public static function getThrottledClient(array $config, RequestLimitRuleset $rules): Client
18
    {
19 3
        $stack = self::_getHandlerStack($config);
20 3
        $stack->unshift((new ThrottleMiddleware($rules))->handle());
21
22 3
        return new Client($config);
23
    }
24
25 3
    private static function _getHandlerStack(array &$config): HandlerStack
26
    {
27 3
        if (!isset($config[self::HANDLER_KEY])) {
28 3
            $stack = HandlerStack::create(new CurlHandler());
29 3
            $config[self::HANDLER_KEY] = $stack;
30
        }
31
32 3
        return $config[self::HANDLER_KEY];
33
    }
34
}
35