CloudApiAuthPlugin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 70
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUsername() 0 4 1
A getPassword() 0 4 1
A getSubscribedEvents() 0 6 1
A onRequestBeforeSend() 0 4 1
A setAuth() 0 4 1
1
<?php
2
3
namespace Acquia\Cloud\Api;
4
5
use Guzzle\Common\Event;
6
use Guzzle\Http\Message\Request;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
/**
10
 * Adds HTTP authentication for the Cloud API.
11
 */
12
class CloudApiAuthPlugin implements EventSubscriberInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $username;
18
19
    /**
20
     * @var string
21
     */
22
    protected $password;
23
24
    /**
25
     * @param string $username
26
     * @param string $password
27
     */
28 162
    public function __construct($username, $password)
29
    {
30 162
        $this->username = $username;
31 162
        $this->password = $password;
32 162
    }
33
34
    /**
35
     * @return string
36
     */
37 3
    public function getUsername()
38
    {
39 3
        return $this->username;
40
    }
41
42
    /**
43
     * @return string
44
     */
45 3
    public function getPassword()
46
    {
47 3
        return $this->password;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 159
    public static function getSubscribedEvents()
54
    {
55
        return array(
56 159
            'request.before_send' => array('onRequestBeforeSend', -1000)
57 159
        );
58
    }
59
60
    /**
61
     * Request before-send event handler.
62
     *
63
     * @param \Guzzle\Common\Event $event
64
     */
65 144
    public function onRequestBeforeSend(Event $event)
66
    {
67 144
        $this->setAuth($event['request']);
68 144
    }
69
70
    /**
71
     * This method seems silly, but it will be really useful if / when the
72
     * authentication scheme becomes more complex. Separating it out from the
73
     * event handler allows us to test this code more easily.
74
     *
75
     * @param \Guzzle\Http\Message\Request $request
76
     */
77 144
    public function setAuth(Request $request)
78
    {
79 144
        $request->setAuth($this->username, $this->password);
80 144
    }
81
}
82