1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ResponseErrorException represent exception occurred during the response processing. |
15
|
|
|
*/ |
16
|
|
|
class ResponseErrorException extends Exception |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ResponseInterface |
20
|
|
|
*/ |
21
|
|
|
protected $response; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ResponseErrorException constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $message the error message |
27
|
|
|
* @param ResponseInterface $response |
28
|
|
|
* @param int $code |
29
|
|
|
* @param \Exception|null $previous |
30
|
|
|
*/ |
31
|
|
|
public function __construct($message, ResponseInterface $response, $code = 0, \Exception $previous = null) |
32
|
|
|
{ |
33
|
|
|
$this->response = $response; |
34
|
|
|
parent::__construct($message, $this->getErrorInfo(), $code, $previous); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return ResponseInterface |
39
|
|
|
*/ |
40
|
|
|
public function getResponse() |
41
|
|
|
{ |
42
|
|
|
return $this->response; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return RequestInterface |
47
|
|
|
*/ |
48
|
|
|
public function getRequest() |
49
|
|
|
{ |
50
|
|
|
return $this->response->getRequest(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function getResponseData() |
57
|
|
|
{ |
58
|
|
|
return $this->getResponse()->getData(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
protected function getErrorInfo() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$request = $this->getRequest(); |
67
|
|
|
$response = $this->getResponse(); |
68
|
|
|
|
69
|
|
|
return [ |
70
|
|
|
'statusCode' => $response->getStatusCode(), |
71
|
|
|
'responseData' => $response->getData(), |
72
|
|
|
'request' => [ |
73
|
|
|
'method' => $request->getMethod(), |
74
|
|
|
'uri' => $request->getFullUri(), |
75
|
|
|
'body' => $request->getBody(), |
76
|
|
|
], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.