RetryOnAuthorizationError   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 12 2
A isUnauthorizedResponse() 0 4 3
1
<?php
2
3
namespace Softonic\OAuth2\Guzzle\Middleware;
4
5
use League\OAuth2\Client\Provider\AbstractProvider as OAuth2Provider;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
class RetryOnAuthorizationError
10
{
11
    private $provider;
12
    private $config;
13
    private $cacheHandler;
14
15 8
    public function __construct(OAuth2Provider $provider, array $config, AccessTokenCacheHandler $cacheHandler)
16
    {
17 8
        $this->provider = $provider;
18 8
        $this->config = $config;
19 8
        $this->cacheHandler = $cacheHandler;
20 8
    }
21
22 4
    public function __invoke(
23
        int $retries,
24
        RequestInterface $request,
25
        ResponseInterface $response = null,
26
        \Exception $exception = null
27
    ): bool {
28 4
        if ($this->isUnauthorizedResponse($retries, $response)) {
29 2
            $this->cacheHandler->deleteItemByProvider($this->provider, $this->config);
30 2
            return true;
31
        }
32 4
        return false;
33
    }
34
35 4
    private function isUnauthorizedResponse(int $retries, ResponseInterface $response = null)
36
    {
37 4
        return !empty($response) && $retries < 1 && $response->getStatusCode() === 401;
38
    }
39
}
40