|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel 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; |
|
15
|
|
|
use yii\base\Application; |
|
16
|
|
|
|
|
17
|
|
|
class Connection extends \hiqdev\hiart\rest\Connection implements ConnectionInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public $queryBuilderClass = QueryBuilder::class; |
|
20
|
|
|
|
|
21
|
|
|
public $commandClass = Command::class; |
|
22
|
|
|
|
|
23
|
|
|
private $app; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Application $app, $config = []) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->app = $app; |
|
28
|
|
|
parent::__construct($config); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param ResponseInterface $response |
|
33
|
|
|
* @return string|false error text or false |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getResponseError(ResponseInterface $response) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!$this->isError($response)) { |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$error = $this->getError($response); |
|
42
|
|
|
if ($error === 'invalid_token') { |
|
43
|
|
|
$this->app->user->logout(); |
|
44
|
|
|
$this->app->response->refresh()->send(); |
|
|
|
|
|
|
45
|
|
|
$this->app->end(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $error ?: 'unknown api error'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param ResponseInterface $response |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function isError(ResponseInterface $response) |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->isRawDataEmpty($response)) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$data = $response->getData(); |
|
62
|
|
|
if ($data === '0') { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return is_array($data) ? array_key_exists('_error', $data) : !$data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function isRawDataEmpty(ResponseInterface $response) |
|
70
|
|
|
{ |
|
71
|
|
|
return $response->getRawData() === null || $response->getRawData() === ''; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function getError(ResponseInterface $response) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->isRawDataEmpty($response)) { |
|
77
|
|
|
return 'The response body is empty'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$data = $response->getData(); |
|
81
|
|
|
if (isset($data['_error'])) { |
|
82
|
|
|
return $data['_error']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets auth data from user. |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getAuth() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->_disabledAuth) { |
|
95
|
|
|
return []; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->app->user->getAuthData(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
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: