AuthListener::onRequestBeforeSend()   C
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 52
Code Lines 34

Duplication

Lines 24
Ratio 46.15 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 24
loc 52
ccs 0
cts 38
cp 0
rs 6.8493
cc 8
eloc 34
nc 8
nop 1
crap 72

How to fix   Long Method   

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 Trello\HttpClient\Listener;
4
5
use Guzzle\Common\Event;
6
use Trello\Client;
7
use Trello\Exception\RuntimeException;
8
9
class AuthListener
10
{
11
    private $tokenOrLogin;
12
    private $password;
13
    private $method;
14
15
    /**
16
     * @param string $tokenOrLogin
17
     * @param string $password
18
     * @param null|string $method
19
     */
20 4
    public function __construct($tokenOrLogin, $password = null, $method)
21
    {
22 4
        $this->tokenOrLogin = $tokenOrLogin;
23 4
        $this->password = $password;
24 4
        $this->method = $method;
25 4
    }
26
27
    public function onRequestBeforeSend(Event $event)
28
    {
29
        // Skip by default
30
        if (null === $this->method) {
31
            return;
32
        }
33
34
        switch ($this->method) {
35
            case Client::AUTH_HTTP_PASSWORD:
36
                $event['request']->setHeader(
37
                    'Authorization',
38
                    sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password))
39
                );
40
                break;
41
42
            case Client::AUTH_HTTP_TOKEN:
43
                $event['request']->setHeader(
44
                    'Authorization',
45
                    sprintf('token %s', $this->tokenOrLogin)
46
                );
47
                break;
48
49 View Code Duplication
            case Client::AUTH_URL_CLIENT_ID:
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...
50
                $url = $event['request']->getUrl();
51
52
                $parameters = array(
53
                    'key'   => $this->tokenOrLogin,
54
                    'token' => $this->password,
55
                );
56
57
                $url .= (false === strpos($url, '?') ? '?' : '&');
58
                $url .= utf8_encode(http_build_query($parameters, '', '&'));
59
60
                $event['request']->setUrl($url);
61
                break;
62
63 View Code Duplication
            case Client::AUTH_URL_TOKEN:
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...
64
                $url = $event['request']->getUrl();
65
                $url .= (false === strpos($url, '?') ? '?' : '&');
66
                $url .= utf8_encode(http_build_query(
67
                    array('token' => $this->tokenOrLogin, 'key' => $this->password),
68
                    '',
69
                    '&'
70
                ));
71
72
                $event['request']->setUrl($url);
73
                break;
74
75
            default:
76
                throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
77
        }
78
    }
79
}
80