|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace linkprofit\Tracker\response; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
|
6
|
|
|
use linkprofit\Tracker\exception\TrackerException; |
|
7
|
|
|
use linkprofit\Tracker\exception\ExceptionHandlerInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ArrayResponseHandler |
|
11
|
|
|
* |
|
12
|
|
|
* @package linkprofit\Tracker |
|
13
|
|
|
*/ |
|
14
|
|
|
class ArrayResponseHandler implements ResponseHandlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
public $decodedBody = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Response |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $response; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ExceptionHandlerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $exceptionHandler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ArrayResponseHandler constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Response|null $response |
|
35
|
|
|
*/ |
|
36
|
11 |
|
public function __construct(Response $response = null) |
|
37
|
|
|
{ |
|
38
|
11 |
|
if ($response !== null) { |
|
39
|
7 |
|
$this->add($response); |
|
40
|
|
|
} |
|
41
|
11 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Response $response |
|
45
|
|
|
*/ |
|
46
|
7 |
|
public function add(Response $response) |
|
47
|
|
|
{ |
|
48
|
7 |
|
$this->response = $response; |
|
49
|
7 |
|
$this->setDecodedBody(); |
|
50
|
7 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array|mixed |
|
54
|
|
|
* @throws TrackerException |
|
55
|
|
|
*/ |
|
56
|
7 |
|
public function handle() |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
7 |
|
$this->validateResponse(); |
|
60
|
2 |
|
} catch (TrackerException $exception) { |
|
61
|
2 |
|
if ($this->exceptionHandler !== null) { |
|
62
|
1 |
|
$this->exceptionHandler->handle($exception->getCode()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
throw $exception; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
6 |
|
return $this->decodedBody; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param ExceptionHandlerInterface $exceptionHandler |
|
73
|
|
|
*/ |
|
74
|
5 |
|
public function setExceptionHandler(ExceptionHandlerInterface $exceptionHandler) |
|
75
|
|
|
{ |
|
76
|
5 |
|
$this->exceptionHandler = $exceptionHandler; |
|
77
|
5 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set $this->decodedBody |
|
81
|
|
|
*/ |
|
82
|
7 |
|
protected function setDecodedBody() |
|
83
|
|
|
{ |
|
84
|
7 |
|
$decodedJson = json_decode($this->response->getBody(), 1); |
|
85
|
7 |
|
$this->decodedBody = is_array($decodedJson) ? $decodedJson : []; |
|
86
|
7 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @throws TrackerException |
|
90
|
|
|
*/ |
|
91
|
7 |
|
protected function validateResponse() |
|
92
|
|
|
{ |
|
93
|
7 |
|
if ($this->response->getStatusCode() !== 200) { |
|
94
|
|
|
throw new TrackerException('Код ответа сервера: ' . $this->response->getStatusCode()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
7 |
|
if (isset($this->decodedBody['code'])) { |
|
98
|
2 |
|
throw new TrackerException('', $this->decodedBody['code']); |
|
99
|
|
|
} |
|
100
|
6 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|