Completed
Push — master ( acb08f...226930 )
by Andrii
05:37
created

Connection::getAuth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
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();
0 ignored issues
show
Bug introduced by
The method refresh does only exist in yii\web\Response, but not in yii\console\Response.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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