AutoBasicAuth   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 34
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A authenticate() 0 14 3
1
<?php
2
3
namespace Http\Message\Authentication;
4
5
use Http\Message\Authentication;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Authenticate a PSR-7 Request using Basic Auth based on credentials in the URI.
10
 *
11
 * @author Márk Sági-Kazár <[email protected]>
12
 */
13
final class AutoBasicAuth implements Authentication
14
{
15
    /**
16
     * Whether user info should be removed from the URI.
17
     *
18
     * @var bool
19
     */
20
    private $shouldRemoveUserInfo;
21
22
    /**
23
     * @param bool|true $shouldRremoveUserInfo
24
     */
25 6
    public function __construct($shouldRremoveUserInfo = true)
26
    {
27 6
        $this->shouldRemoveUserInfo = (bool) $shouldRremoveUserInfo;
28 6
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 4
    public function authenticate(RequestInterface $request)
34
    {
35 4
        $uri = $request->getUri();
36 4
        $userInfo = $uri->getUserInfo();
37
38 4
        if (!empty($userInfo)) {
39 3
            if ($this->shouldRemoveUserInfo) {
40 2
                $request = $request->withUri($uri->withUserInfo(''));
41
            }
42
43 3
            $request = $request->withHeader('Authorization', sprintf('Basic %s', base64_encode($userInfo)));
44
        }
45
46 4
        return $request;
47
    }
48
}
49