Completed
Push — master ( 0728e2...7bf439 )
by Tomas
10:58 queued 10s
created

HeaderApiKeyAuthentication::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Authorization;
6
7
use Tomaj\NetteApi\Misc\IpDetectorInterface;
8
use Tomaj\NetteApi\Misc\TokenRepositoryInterface;
9
10
class HeaderApiKeyAuthentication extends TokenAuthorization
11
{
12
    private $headerName;
13
14
    public function __construct(string $headerName, TokenRepositoryInterface $tokenRepository, IpDetectorInterface $ipDetector)
15
    {
16
        parent::__construct($tokenRepository, $ipDetector);
17
        $this->headerName = $headerName;
18
    }
19
20
    protected function readAuthorizationToken(): ?string
21
    {
22
        $headerName = 'HTTP_' . strtoupper(str_replace('-', '_', $this->headerName));
23
        $apiKey = $_SERVER[$headerName] ?? null;
24
        if (!$apiKey) {
25
            $this->errorMessage = 'API key is not set';
26
            return null;
27
        }
28
        return $apiKey;
29
    }
30
31
    public function getHeaderName(): string
32
    {
33
        return $this->headerName;
34
    }
35
}
36