Completed
Push — master ( ee7335...233299 )
by Andrii
13:05
created

NopeClient::getState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 NopeClient.
16
 * Allows OAuth2 authentication through Nope server.
17
 *
18
 * In order to use NOPE client you must register your application at NOPE 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
 *             'nope' => [
28
 *                 'class'        => \hiam\authclient\NopeClient::class,
29
 *                 'site'         => 'nope.somewhere.com',
30
 *                 'clientId'     => 'client_id',
31
 *                 'clientSecret' => 'client_secret',
32
 *             ],
33
 *         ],
34
 *     ]
35
 *     ...
36
 * ]
37
 * ```
38
 */
39 View Code Duplication
class NopeClient extends \yii\authclient\OAuth2
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
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
            throw new \Exception('Nope site must not be empty!');
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 NopeClient::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 'nope';
108
    }
109
110
    /** {@inheritdoc} */
111
    protected function defaultTitle()
112
    {
113
        return 'nope';
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