|
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
|
|
|
$lastJump = end($this->jumpResponses); |
|
73
|
|
|
|
|
74
|
|
|
if ($lastJump === false) { |
|
75
|
|
|
throw new \RuntimeException('Unable to retrieve last jump.'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $lastJump; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getLastResponseAsString(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return Message::toString($this->getLastResponse()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function __toString(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return parent::__toString() . PHP_EOL |
|
89
|
|
|
. 'Http log:' . PHP_EOL |
|
90
|
|
|
. '>>>' . PHP_EOL . $this->getRequestAsString() . PHP_EOL |
|
91
|
|
|
. '<<<' . PHP_EOL . $this->getLastResponseAsString() |
|
92
|
|
|
; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|