Completed
Push — master ( 27ffc6...487ef5 )
by Dmitry
05:42
created

Connection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 6.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 5
loc 74
ccs 0
cts 50
cp 0
rs 10
wmc 16
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
B checkError() 0 17 5
A isError() 0 4 2
A getError() 0 4 2
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' && $this->isError($response)) {
27
            $error = $this->getError($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
    private function isError($data)
43
    {
44
        return is_array($data) ? array_key_exists('_error', $data) : !$data;
45
    }
46
47
    private function getError($data)
48
    {
49
        return isset($data['_error']) ? $data['_error'] : null;
50
    }
51
52
    /**
53
     * Prepares authorization data.
54
     * If user is not authorized redirects to authorization.
55
     * @return array
56
     */
57
    public function getAuth()
58
    {
59
        if ($this->_disabledAuth) {
60
            return [];
61
        }
62
63
        $user  = Yii::$app->user;
64
65
        $identity = $user->identity;
66
        if ($identity === null) {
67
            Yii::$app->response->redirect('/site/logout');
68
            Yii::$app->end();
69
        }
70
71
        $token = $identity->getAccessToken();
72
        if (!$token && $user->loginRequired() !== null) {
73
            Yii::$app->response->redirect('/site/logout');
74
            Yii::$app->end();
75
        }
76
77
        if ($user->identity) {
78
            return ['access_token' => $token];
79
        }
80
81 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...
82
            Yii::trace('Login is required. Redirecting to the login page', 'hipanel');
83
            Yii::$app->response->send();
84
            Yii::$app->end();
85
        }
86
87
        return [];
88
    }
89
}
90