Completed
Pull Request — master (#2)
by Gregorio
03:07
created

Http::setAuth()   C

Complexity

Conditions 11
Paths 22

Size

Total Lines 62
Code Lines 53

Duplication

Lines 13
Ratio 20.97 %

Importance

Changes 0
Metric Value
dl 13
loc 62
rs 6.1722
c 0
b 0
f 0
cc 11
eloc 53
nc 22
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ghc\Rosetta\Connectors;
4
5
use Ghc\Rosetta\Messages\HttpResponse;
6
use gomes81\GuzzleHttp\Subscriber\CookieAuth;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Subscriber\Oauth\Oauth1;
10
use kamermans\OAuth2\OAuth2Middleware;
11
12
class Http extends Connector
13
{
14
    /**
15
     * Basic HTTP auth.
16
     * Config example: ['type' => self::AUTH_BASIC, 'username' => 'USER', 'password' => 'PASSWD']
17
     */
18
    const AUTH_BASIC = 'basic';
19
20
    /**
21
     * Digest HTTP auth.
22
     * Config example: ['type' => self::AUTH_DIGEST, 'username' => 'USER', 'password' => 'PASSWD']
23
     */
24
    const AUTH_DIGEST = 'digest';
25
26
    /**
27
     * Microsoft NTLM HTTP auth.
28
     * Config example: ['type' => self::AUTH_NTLM, 'username' => 'USER', 'password' => 'PASSWD']
29
     */
30
    const AUTH_NTLM = 'ntlm';
31
32
    /**
33
     * Oauth1 auth.
34
     * Config example: ['type' => self::AUTH_OAUTH1, 'consumer_key' => 'my_key', 'consumer_secret' => 'my_secret', 'token' => 'my_token', 'token_secret' => 'my_token_secret']
35
     * @link https://github.com/guzzle/oauth-subscriber
36
     */
37
    const AUTH_OAUTH1 = 'oauth1';
38
39
    /**
40
     * Oauth2 auth.
41
     * Config example: ['type' => self::AUTH_OAUTH2, "uri" => "access_token_uri", "grant_type" => "client_credentials", "client_id" => "my_client_id", "client_secret" => "my_client_secret",]
42
     * Grant types: authorization_code, client_credentials, password_credentials, refresh_token
43
     * @link https://github.com/kamermans/guzzle-oauth2-subscriber
44
     */
45
    const AUTH_OAUTH2 = 'oauth2';
46
47
    /**
48
     * Cookie auth.
49
     * Config example: ['type' => self::AUTH_COOKIE, 'uri' => 'form_uri', 'fields' => ['username' => 'USER', 'password' => 'PASSWD', 'foo' => 'bar'], 'method' => 'POST', 'cookies' => 'cookie_string_or_cookie_string_array_or_cookie_jar']
50
     */
51
    const AUTH_COOKIE = 'cookie';
52
53
    /**
54
     * Custom handler stack auth.
55
     * Config example: ['type' => self::AUTH_CUSTOM, 'handler' => $handlerStack', 'auth' => 'authName']
56
     */
57
    const AUTH_CUSTOM = 'custom';
58
59
    /**
60
     * HTTP client.
61
     *
62
     * @var Client
63
     */
64
    protected $client;
65
66
    /**
67
     * Boot Connector.
68
     */
69
    protected function boot()
70
    {
71
    }
72
73
    /**
74
     * @param Client $client
75
     *
76
     * @return Http
77
     */
78
    public function setClient($client)
79
    {
80
        $this->client = $client;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return Client
87
     */
88
    public function getClient()
89
    {
90
        if (!$this->client) {
91
            $this->bootClient();
92
        }
93
94
        return $this->client;
95
    }
96
97
    /**
98
     * @return Client
99
     */
100
    public function bootClient()
101
    {
102
        if ($authConfig = $this->config->get('auth_config')) {
103
            $this->config->set('auth_config', null);
104
            $this->setAuth($authConfig);
105
        }
106
107
        $this->setClient(new Client($this->config->all()));
108
    }
109
110
    public function setAuth($config = [])
111
    {
112
        $clientConfig = [];
113
        $type = $config['type'];
114
        unset($config['type']);
115
116
        switch ($type) {
117 View Code Duplication
            case self::AUTH_BASIC:
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...
118
                $clientConfig['auth'] = [
119
                    $config['username'],
120
                    $config['password'],
121
                ];
122
                break;
123
            case self::AUTH_DIGEST:
124 View Code Duplication
            case self::AUTH_NTLM:
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...
125
                $clientConfig['auth'] = [
126
                    $config['username'],
127
                    $config['password'],
128
                    $type
129
                ];
130
                break;
131
            case self::AUTH_OAUTH1:
132
                $stack = HandlerStack::create();
133
                $middleware = new Oauth1($config);
134
                $stack->push($middleware);
135
                $clientConfig['handler'] = $stack;
136
                $clientConfig['auth'] = 'oauth';
137
                break;
138
            case self::AUTH_OAUTH2:
139
                $authClient = new Client(['base_uri' => $config['uri'],]);
140
                $grantType = '\\kamermans\\OAuth2\\GrantType\\' . studly_case($config['grant_type']);
141
                unset($config['uri']);
142
                unset($config['grant_type']);
143
                /** @var \kamermans\OAuth2\GrantType\GrantTypeInterface $grant_type */
144
                $grant_type = new $grantType($authClient, $config);
145
                $oauth = new OAuth2Middleware($grant_type);
146
                $stack = HandlerStack::create();
147
                $stack->push($oauth);
148
                $clientConfig['handler'] = $stack;
149
                $clientConfig['auth'] = 'oauth';
150
                break;
151
            case self::AUTH_COOKIE:
152
                $stack = HandlerStack::create();
153
                if (!isset($config['cookies'])) $config['cookies'] = null;
154
                if (!isset($config['method'])) $config['method'] = 'POST';
155
                $middleware = new CookieAuth(
156
                    $config['uri'],
157
                    $config['fields'],
158
                    $config['method'],
159
                    $config['cookies']
160
                );
161
                $stack->push($middleware);
162
                $clientConfig['handler'] = $stack;
163
                $clientConfig['auth'] = 'cookie';
164
                break;
165
            case self::AUTH_CUSTOM:
166
                $clientConfig['handler'] = $config['handler'];
167
                $clientConfig['auth'] = $config['auth'];
168
        }
169
170
        foreach ($clientConfig as $key => $value) {
171
            $this->setConfig($key, $value);
172
        }
173
    }
174
175
    /**
176
     * @param string $uri
177
     * @param array  $options
178
     *
179
     * @return mixed
180
     */
181
    public function show($uri, $options = [])
182
    {
183
        return new HttpResponse($this->getClient()->request('GET', $uri, $options));
184
    }
185
186
    /**
187
     * @param string     $uri
188
     * @param null|mixed $data
189
     * @param array      $options
190
     *
191
     * @return mixed
192
     */
193 View Code Duplication
    public function create($uri, $data = null, $options = [])
0 ignored issues
show
Duplication introduced by
This method 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...
194
    {
195
        if (!is_null($data)) {
196
            $options['form_params'] = $data;
197
        }
198
199
        return new HttpResponse($this->getClient()->request('POST', $uri, $options));
200
    }
201
202
    /**
203
     * @param string     $uri
204
     * @param null|mixed $data
205
     * @param array      $options
206
     *
207
     * @return mixed
208
     */
209 View Code Duplication
    public function update($uri, $data = null, $options = [])
0 ignored issues
show
Duplication introduced by
This method 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...
210
    {
211
        if (!is_null($data)) {
212
            $options['form_params'] = $data;
213
        }
214
215
        return new HttpResponse($this->getClient()->request('PATCH', $uri, $options));
216
    }
217
218
    /**
219
     * @param string     $uri
220
     * @param null|mixed $data
221
     * @param array      $options
222
     *
223
     * @return mixed
224
     */
225 View Code Duplication
    public function replace($uri, $data = null, $options = [])
0 ignored issues
show
Duplication introduced by
This method 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...
226
    {
227
        if (!is_null($data)) {
228
            $options['form_params'] = $data;
229
        }
230
231
        return new HttpResponse($this->getClient()->request('PUT', $uri, $options));
232
    }
233
234
    /**
235
     * @param string $uri
236
     * @param array  $options
237
     *
238
     * @return mixed
239
     */
240
    public function delete($uri, $options = [])
241
    {
242
        return new HttpResponse($this->getClient()->request('DELETE', $uri, $options));
243
    }
244
}
245