ClientHelper::_getHandlerStack()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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