Passed
Push — master ( 45618f...b8b318 )
by Dmitry
13:17
created

RequestSenderTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 30 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi;
6
7
use GuzzleHttp\Psr7\Uri;
8
use Promopult\TikTokMarketingApi\Exception\TooManyJumpsException;
9
use Psr\Http\Client\ClientExceptionInterface;
10
use Psr\Http\Client\ClientInterface;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * Trait RequestSenderTrait
16
 *
17
 * @property ClientInterface $httpClient
18
 */
19
trait RequestSenderTrait
20
{
21
    /**
22
     * @throws ClientExceptionInterface
23
     */
24
    protected function sendRequest(
25
        RequestInterface $request,
26
        int $maxRedirectJumps = 5
27
    ): ResponseInterface {
28
        $responses = [];
29
30
        jump:
31
32
        $response = $this->httpClient->sendRequest($request);
33
34
        $redirectCodes = [307];
35
36
        if (in_array($response->getStatusCode(), $redirectCodes)) {
37
            $responses[] = $response;
38
39
            if (count($responses) >= $maxRedirectJumps) {
40
                throw new TooManyJumpsException($request, $responses);
41
            }
42
43
            $uri = current($response->getHeader('Location'));
44
45
            /**
46
             * @psalm-suppress UnusedVariable
47
             */
48
            $request = $request->withUri(new Uri($uri));
49
50
            goto jump;
51
        }
52
53
        return $response;
54
    }
55
}
56