AuthListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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