TokenExtractor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 15 5
A __construct() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSExtractor;
12
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Class TokenExtractor.
17
 *
18
 * @author Jafar Jabr <[email protected]>
19
 */
20
final class TokenExtractor implements TokenExtractorInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $prefix;
26
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @param string $prefix
34
     * @param string $name
35
     */
36
    public function __construct(string $prefix, string $name)
37
    {
38
        $this->prefix = $prefix;
39
        $this->name   = $name;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function extract(Request $request)
46
    {
47
        if (!$request->headers->has($this->name)) {
48
            return false;
49
        }
50
        $authorizationHeader = $request->headers->get($this->name);
51
        if (empty($this->prefix)) {
52
            return $authorizationHeader;
53
        }
54
        $headerParts = explode(' ', $authorizationHeader);
55
        if (!(2 === count($headerParts) && $headerParts[0] === $this->prefix)) {
56
            return false;
57
        }
58
59
        return $headerParts[1];
60
    }
61
}
62