1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel API client made with HiART. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-hiart |
6
|
|
|
* @package hipanel-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\hiart; |
12
|
|
|
|
13
|
|
|
use hiqdev\hiart\ResponseInterface; |
14
|
|
|
use yii\base\Application; |
15
|
|
|
|
16
|
|
|
class Connection extends \hiqdev\hiart\rest\Connection implements ConnectionInterface |
17
|
|
|
{ |
18
|
|
|
public $queryBuilderClass = QueryBuilder::class; |
19
|
|
|
|
20
|
|
|
private $app; |
21
|
|
|
|
22
|
|
|
public function __construct(Application $app, $config = []) |
23
|
|
|
{ |
24
|
|
|
$this->app = $app; |
25
|
|
|
parent::__construct($config); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Fixes response. |
30
|
|
|
* @param ResponseInterface $response |
31
|
|
|
* @return bool if response was fixed |
32
|
|
|
*/ |
33
|
|
|
public function fixResponse(ResponseInterface $response) |
34
|
|
|
{ |
35
|
|
|
if ($response->getRequest()->getQuery()->getOption('batch')) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($response->getData() !== []) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$class = new \ReflectionObject($response); |
44
|
|
|
$prop = $class->getProperty('data'); |
45
|
|
|
$prop->setAccessible(true); |
46
|
|
|
$prop->setValue($response, null); |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Calls fixResponse. |
53
|
|
|
* @param ResponseInterface $response |
54
|
|
|
* @return string|false error text or false |
55
|
|
|
*/ |
56
|
|
|
public function getResponseError(ResponseInterface $response) |
57
|
|
|
{ |
58
|
|
|
if ($this->fixResponse($response)) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$this->isError($response)) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$error = $this->getError($response); |
67
|
|
|
if ($error === 'invalid_token') { |
68
|
|
|
$this->app->user->logout(); |
69
|
|
|
$this->app->response->refresh()->send(); |
|
|
|
|
70
|
|
|
$this->app->end(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $error ?: 'unknown api error'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ResponseInterface $response |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isError(ResponseInterface $response) |
81
|
|
|
{ |
82
|
|
|
if ($this->isRawDataEmpty($response)) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$data = $response->getData(); |
87
|
|
|
if ($data === '0') { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return is_array($data) ? array_key_exists('_error', $data) : !$data; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function isRawDataEmpty(ResponseInterface $response) |
95
|
|
|
{ |
96
|
|
|
return $response->getRawData() === null || $response->getRawData() === ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getError(ResponseInterface $response) |
100
|
|
|
{ |
101
|
|
|
if ($this->isRawDataEmpty($response)) { |
102
|
|
|
return 'The response body is empty'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$data = $response->getData(); |
106
|
|
|
if (isset($data['_error'])) { |
107
|
|
|
return $data['_error']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets auth data from user. |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getAuth() |
118
|
|
|
{ |
119
|
|
|
if ($this->_disabledAuth) { |
120
|
|
|
return []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->app->user->getAuthData(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: