HeaderExtractor::extractKey()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Extractor;
6
7
use Symfony\Component\HttpFoundation\Request;
8
9
final class HeaderExtractor implements Extractor
10
{
11
    private $name;
12
    private $prefix;
13
14
    public function __construct(string $name, string $prefix = null)
15
    {
16
        $this->name = $name;
17
        $this->prefix = $prefix;
18
    }
19
20
    public function extractKey(Request $request): ?string
21
    {
22
        $value = $request->headers->get($this->name);
23
24
        if (!$value || !$this->prefix) {
25
            return $value;
26
        }
27
28
        if (0 !== strpos($value, $this->prefix . ' ')) {
29
            return null;
30
        }
31
32
        return substr($value, strlen($this->prefix) + 1);
33
    }
34
}
35