Passed
Pull Request — master (#116)
by Rustam
01:56
created

HttpHeaderAuthTrait::getAuthToken()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 0
cts 15
cp 0
crap 20
rs 9.9332
1
<?php
2
namespace Yiisoft\Yii\Web\Auth;
3
4
use Psr\Http\Message\ServerRequestInterface;
5
6
trait HttpHeaderAuthTrait
7
{
8
    /**
9
     * @var string the HTTP header name
10
     */
11
    private $header = 'X-Api-Key';
12
    /**
13
     * @var string a pattern to use to extract the HTTP authentication value
14
     */
15
    private $pattern;
16
17
    private function getAuthToken(ServerRequestInterface $request): ?string
18
    {
19
        $authHeaders = $request->getHeader($this->header);
20
        $authHeader = \reset($authHeaders);
21
        if ($authHeader !== null) {
22
            if ($this->pattern !== null) {
23
                if (preg_match($this->pattern, $authHeader, $matches)) {
24
                    $authHeader = $matches[1];
25
                } else {
26
                    return null;
27
                }
28
            }
29
            return $authHeader;
30
        }
31
        return null;
32
    }
33
}