RetryOnAuthorizationError::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 2
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