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

TooManyJumpsException::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi\Exception;
6
7
use GuzzleHttp\Psr7\Message;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class TooManyJumpsException extends \RuntimeException
12
{
13
    private RequestInterface $request;
14
    /**
15
     * @var ResponseInterface[]
16
     */
17
    private array $jumpResponses;
18
19
    /**
20
     * @param RequestInterface $request
21
     * @param array<int,ResponseInterface> $jumpResponses
22
     * @param string $message
23
     * @param int $code
24
     * @param \Throwable|null $previous
25
     */
26
    public function __construct(
27
        RequestInterface $request,
28
        array $jumpResponses,
29
        string $message = 'Too many redirect jumps.',
30
        int $code = 0,
31
        \Throwable $previous = null
32
    ) {
33
        parent::__construct($message, $code, $previous);
34
        $this->request = $request;
35
        $this->jumpResponses = $jumpResponses;
36
    }
37
38
    /**
39
     * @return RequestInterface
40
     */
41
    public function getRequest(): RequestInterface
42
    {
43
        return $this->request;
44
    }
45
46
    public function getRequestAsString(): string
47
    {
48
        return Message::toString($this->request);
49
    }
50
51
    public function getResponseJumpsAsString(): string
52
    {
53
        $jumpsStr = '';
54
55
        foreach ($this->jumpResponses as $response) {
56
            $jumpsStr .= Message::toString($response) . PHP_EOL . '---' . PHP_EOL;
57
        }
58
59
        return $jumpsStr;
60
    }
61
62
    /**
63
     * @return ResponseInterface[]
64
     */
65
    public function getJumpResponses(): array
66
    {
67
        return $this->jumpResponses;
68
    }
69
70
    public function getLastResponse(): ResponseInterface
71
    {
72
        return end($this->jumpResponses);
73
    }
74
75
    public function getLastResponseAsString(): string
76
    {
77
        return Message::toString($this->getLastResponse());
78
    }
79
80
    public function __toString(): string
81
    {
82
        return parent::__toString() . PHP_EOL
83
            . 'Http log:' . PHP_EOL
84
            . '>>>' . PHP_EOL . $this->getRequestAsString() . PHP_EOL
85
            . '<<<' . PHP_EOL . $this->getLastResponseAsString()
86
        ;
87
    }
88
}
89