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

Connection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 7.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 64
ccs 0
cts 42
cp 0
rs 10
wmc 12
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B checkError() 0 17 5
C getAuth() 5 32 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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