Completed
Push — master ( d69358...27ffc6 )
by Dmitry
04:53 queued 53s
created

Connection::checkError()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 0
cts 16
cp 0
crap 30
rs 8.8571
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\components;
13
14
use Yii;
15
16
class Connection extends \hiqdev\hiart\Connection
17
{
18
    /**
19
     * @param mixed $response The response
20
     * @return null|string
21
     *  - string: the error text
22
     *  - null: the response is not an error
23
     */
24
    public function checkError($response)
25
    {
26
        if ($response !== '0' && Err::is($response)) {
27
            $error = Err::get($response);
28
            if (empty($error)) {
29
                return 'unknown api error';
30
            } elseif ($error === 'invalid_token') {
31
                Yii::$app->user->logout();
32
                Yii::$app->response->refresh()->send();
33
                Yii::$app->end();
34
            } else {
35
                return $error;
36
            }
37
        }
38
39
        return null;
40
    }
41
42
    /**
43
     * Prepares authorization data.
44
     * If user is not authorized redirects to authorization.
45
     * @return array
46
     */
47
    public function getAuth()
48
    {
49
        if ($this->_disabledAuth) {
50
            return [];
51
        }
52
53
        $user  = Yii::$app->user;
54
55
        $identity = $user->identity;
56
        if ($identity === null) {
57
            Yii::$app->response->redirect('/site/logout');
58
            Yii::$app->end();
59
        }
60
61
        $token = $identity->getAccessToken();
62
        if (!$token && $user->loginRequired() !== null) {
63
            Yii::$app->response->redirect('/site/logout');
64
            Yii::$app->end();
65
        }
66
67
        if ($user->identity) {
68
            return ['access_token' => $token];
69
        }
70
71 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...
72
            Yii::trace('Login is required. Redirecting to the login page', 'hipanel');
73
            Yii::$app->response->send();
74
            Yii::$app->end();
75
        }
76
77
        return [];
78
    }
79
}
80