ServerRequestAuthBearerProvider::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Wallit\Token\Provider;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class ServerRequestAuthBearerProvider implements ServerRequestProviderInterface
10
{
11
    /**
12
     * @var string[]
13
     */
14
    public const DEFAULT_OPTIONS = [
15
        self::OPTION_HTTP_HEADER        => 'Authentication',
16
        self::OPTION_HTTP_HEADER_PREFIX => 'Bearer '
17
    ];
18
19
    /**
20
     * @var string
21
     */
22
    public const OPTION_HTTP_HEADER = 'httpHeader';
23
24
    /**
25
     * @var string
26
     */
27
    public const OPTION_HTTP_HEADER_PREFIX = 'httpHeaderPrefix';
28
29
    /**
30
     * @var ServerRequestInterface
31
     */
32
    private $request;
33
34
    /**
35
     * @var bool
36
     */
37
    private $loaded = false;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $tokenString;
43
44
    /**
45
     * @var string
46
     */
47
    private $httpHeader;
48
49
    /**
50
     * @var string
51
     */
52
    private $httpHeaderPrefix;
53
54
    /**
55
     * HttpAuthenticationBearer constructor.
56
     *
57
     * @throws \InvalidArgumentException
58
     *
59
     * @param ServerRequestInterface $request
60
     * @param string[]               $options
61
     */
62 16
    public function __construct(ServerRequestInterface $request, array $options = [])
63
    {
64 16
        $this->request = $request;
65
66 16
        $this->httpHeader = trim($options['httpHeader'] ?? self::DEFAULT_OPTIONS['httpHeader']);
67 16
        $this->httpHeaderPrefix = ($options['httpHeaderPrefix'] ?? self::DEFAULT_OPTIONS['httpHeaderPrefix']);
68
69 16
        if ($this->httpHeader === '') {
70 1
            throw new \InvalidArgumentException('httpHeader option cannot be empty.');
71
        }
72 15
    }
73
74
    /**
75
     * @return bool
76
     */
77 5
    public function hasToken(): bool
78
    {
79 5
        if (!$this->loaded) {
80 5
            $this->loadTokenFromRequest();
81
        }
82
83 5
        return $this->tokenString !== null;
84
    }
85
86
    /**
87
     * Return token string.
88
     *
89
     * @return string|null
90
     */
91 13
    public function getPlainToken(): ?string
92
    {
93 13
        if (!$this->loaded) {
94 10
            $this->loadTokenFromRequest();
95
        }
96
97 13
        return $this->tokenString;
98
    }
99
100 15
    protected function loadTokenFromRequest(): void
101
    {
102 15
        $headers = $this->request->getHeader($this->httpHeader);
103 15
        foreach ($headers as $header) {
104 9
            if ($this->httpHeaderPrefix !== '') {
105 8
                if (strpos($header, $this->httpHeaderPrefix) === 0) {
106 8
                    $this->tokenString = trim(str_replace($this->httpHeaderPrefix, '', $header));
107
                }
108
            } else {
109 9
                $this->tokenString = trim($header);
110
            }
111
        }
112 15
        $this->loaded = true;
113 15
    }
114
}
115