Completed
Push — master ( dba3c8...01cd01 )
by Jean C.
07:48 queued 01:45
created

BasicAuth   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A register() 0 4 1
A before_request() 0 4 1
1
<?php
2
3
namespace Moip\Auth;
4
5
use Moip\Contracts\Authentication;
6
use Requests_Hooks;
7
8
/**
9
 * Class BasicAuth.
10
 */
11
class BasicAuth implements Authentication
12
{
13
    /**
14
     * Token.
15
     *
16
     * @var string
17
     */
18
    private $token;
19
20
    /**
21
     * Access key.
22
     *
23
     * @var string
24
     */
25
    private $key;
26
27
    /**
28
     * Create a new MoipBasic instance.
29
     *
30
     * @param string $token
31
     * @param string $key
32
     */
33
    public function __construct($token, $key)
34
    {
35
        $this->token = $token;
36
        $this->key = $key;
37
    }
38
39
    /**
40
     * Register hooks as needed.
41
     *
42
     * This method is called in {@see Requests::request} when the user has set
43
     * an instance as the 'auth' option. Use this callback to register all the
44
     * hooks you'll need.
45
     *
46
     * @see \Requests_Hooks::register
47
     *
48
     * @param \Requests_Hooks $hooks Hook system
49
     */
50
    public function register(Requests_Hooks &$hooks)
51
    {
52
        $hooks->register('requests.before_request', [&$this, 'before_request']);
53
    }
54
55
    /**
56
     * Sets the authentication header.
57
     *
58
     * @param string       $url
59
     * @param array        $headers
60
     * @param array|string $data
61
     * @param string       $type
62
     * @param array        $options
63
     */
64
    public function before_request(&$url, &$headers, &$data, &$type, &$options)
65
    {
66
        $headers['Authorization'] = 'Basic '.base64_encode($this->token.':'.$this->key);
67
    }
68
}
69