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

HttpHeaderAuthTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAuthToken() 0 15 4
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
}