HiamClient   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 101
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A buildUrl() 0 6 2
A buildAuthUrl() 0 6 1
A init() 0 20 5
A initUserAttributes() 0 4 1
A apiInternal() 0 9 2
A defaultName() 0 4 1
A defaultTitle() 0 4 1
A setState() 0 4 1
A getState() 0 4 1
A removeState() 0 4 1
1
<?php
2
3
/*
4
 * OAuth2 client for yii2 to login with HIAM server
5
 *
6
 * @link      https://github.com/hiqdev/yii2-hiam-authclient
7
 * @package   yii2-hiam-authclient
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiam\authclient;
13
14
/**
15
 * Class HiamClient.
16
 * Allows OAuth2 authentication through HIAM server.
17
 *
18
 * In order to use HIAM client you must register your application at HIAM server.
19
 * Used identical to yii2-authclient Oauth2Client.
20
 *
21
 * Example application configuration:
22
 * ```
23
 * 'components' => [
24
 *     'authClientCollection' => [
25
 *         'class' => \hiam\authclient\Collection::class,
26
 *         'clients' => [
27
 *             'hiam' => [
28
 *                 'class'        => \hiam\authclient\HiamClient::class,
29
 *                 'site'         => 'hiam.hipanel.com',
30
 *                 'clientId'     => 'client_id',
31
 *                 'clientSecret' => 'client_secret',
32
 *             ],
33
 *         ],
34
 *     ]
35
 *     ...
36
 * ]
37
 * ```
38
 */
39
class HiamClient extends \yii\authclient\OAuth2
40
{
41
    /**
42
     * Site for urls generation.
43
     */
44
    public $site;
45
46
    public function buildUrl($path, array $params = [])
47
    {
48
        $url = $this->site . '/' . $path;
49
50
        return $params ? $this->composeUrl($url, $params) : $url;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function buildAuthUrl(array $params = [])
57
    {
58
        $params['language'] = \Yii::$app->language;
59
60
        return parent::buildAuthUrl($params);
61
    }
62
63
    /**
64
     * Inits Urls based on $site.
65
     */
66
    public function init()
67
    {
68
        parent::init();
69
        if (!$this->site) {
70
            $this->site = 'hiam.hipanel.com';
71
        }
72
        if (strpos($this->site, '://') === false) {
73
            $this->site = 'https://' . $this->site;
74
        }
75
        $defaults = [
76
            'authUrl' => 'oauth/authorize',
77
            'tokenUrl' => 'oauth/token',
78
            'apiBaseUrl' => 'api',
79
        ];
80
        foreach ($defaults as $k => $v) {
81
            if (!$this->{$k}) {
82
                $this->{$k} = $this->buildUrl($v);
83
            }
84
        }
85
    }
86
87
    /** {@inheritdoc} */
88
    protected function initUserAttributes()
89
    {
90
        return $this->getAccessToken()->getParam('user_attributes');
91
    }
92
93
    /** {@inheritdoc} */
94
    protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
95
    {
96
        if (!isset($params['format'])) {
97
            $params['format'] = 'json';
98
        }
99
        $params['oauth_token'] = $accessToken->getToken();
100
101
        return $this->sendRequest($method, $url, $params, $headers);
0 ignored issues
show
Unused Code introduced by
The call to HiamClient::sendRequest() has too many arguments starting with $url.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
102
    }
103
104
    /** {@inheritdoc} */
105
    protected function defaultName()
106
    {
107
        return 'hiam';
108
    }
109
110
    /** {@inheritdoc} */
111
    protected function defaultTitle()
112
    {
113
        return 'hiam';
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function setState($key, $value)
120
    {
121
        return parent::setState($key, $value);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getState($key)
128
    {
129
        return parent::getState($key);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function removeState($key)
136
    {
137
        return parent::removeState($key);
138
    }
139
}
140