BasicAuthorization::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace DMT\Auth\Authorization;
4
5
use DMT\Auth\AuthorizationInterface;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Class BasicAuthorization
10
 *
11
 * @package DMT\Auth
12
 */
13
class BasicAuthorization implements AuthorizationInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $user;
19
20
    /**
21
     * @var string
22
     */
23
    protected $password;
24
25
    /**
26
     * BasicAuthorization constructor.
27
     *
28
     * @param string $user
29
     * @param string $password
30
     */
31 1
    public function __construct(string $user, string $password)
32
    {
33 1
        $this->user = $user;
34 1
        $this->password = $password;
35 1
    }
36
37
    /**
38
     * Get the http headers associated with the authorization.
39
     *
40
     * @param RequestInterface $request
41
     * @return RequestInterface
42
     */
43 1
    public function handle(RequestInterface $request): RequestInterface
44
    {
45
        return
46 1
            $request->withHeader(
47 1
                'Authorization',
48 1
                sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->user, $this->password)))
49
            );
50
    }
51
}
52