AuthListener   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 33.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 11.63%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 9
c 1
b 1
f 1
lcom 1
cbo 1
dl 24
loc 71
ccs 5
cts 43
cp 0.1163
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C onRequestBeforeSend() 24 52 8

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