Completed
Push — master ( 8b71e8...ad4a7a )
by Andrii
03:58
created

Connection::getAuth()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26
Code Lines 15

Duplication

Lines 5
Ratio 19.23 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 15
c 1
b 1
f 0
nc 7
nop 0
dl 5
loc 26
ccs 0
cts 21
cp 0
crap 42
rs 8.439
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\base;
13
14
use Yii;
15
16
class Connection extends \hiqdev\hiart\Connection
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function init()
22
    {
23
        $this->errorChecker = [$this, 'checkError'];
0 ignored issues
show
Bug introduced by
The property errorChecker does not seem to exist. Did you mean _errorChecker?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
24
    }
25
26
    /**
27
     * @param mixed $response The response
28
     * @return null|string
29
     *  - string: the error text
30
     *  - null: the response is not an error
31
     */
32
    public function checkError($response)
33
    {
34
        if ($response !== '0' && Err::is($response)) {
35
            $error = Err::get($response);
36
            if (empty($error)) {
37
                return 'unknown api error';
38
            } elseif ($error === 'invalid_token') {
39
                Yii::$app->user->logout();
40
                Yii::$app->response->refresh()->send();
41
                Yii::$app->end();
42
            } else {
43
                return $error;
44
            }
45
        }
46
47
        return null;
48
    }
49
50
    public function getAuth()
51
    {
52
        if ($this->_disabledAuth) {
53
            return [];
54
        }
55
56
        $user  = Yii::$app->user;
57
        $token = $user->identity->getAccessToken();
58
59
        if (!$token && $user->loginRequired() !== null) {
60
            Yii::$app->response->redirect('/site/logout');
61
            Yii::$app->end();
62
        }
63
64
        if ($user->identity) {
65
            return ['access_token' => $token];
66
        }
67
68 View Code Duplication
        if ($user->loginRequired() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
69
            Yii::trace('Login is required. Redirecting to the login page', 'hipanel');
70
            Yii::$app->response->send();
71
            Yii::$app->end();
72
        }
73
74
        return [];
75
    }
76
}
77