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

Connection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 8.2 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 5
loc 61
ccs 0
cts 41
cp 0
rs 10
wmc 12
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B checkError() 0 17 5
B getAuth() 5 26 6

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\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