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

CookieApiKeyAuthentication::__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 CookieApiKeyAuthentication extends TokenAuthorization
11
{
12
    private $cookieName;
13
14
    public function __construct(string $cookieName, TokenRepositoryInterface $tokenRepository, IpDetectorInterface $ipDetector)
15
    {
16
        parent::__construct($tokenRepository, $ipDetector);
17
        $this->cookieName = $cookieName;
18
    }
19
20
    protected function readAuthorizationToken(): ?string
21
    {
22
        $apiKey = $_COOKIE[$this->cookieName] ?? null;
23
        if (!$apiKey) {
24
            $this->errorMessage = 'API key is not set';
25
            return null;
26
        }
27
        return $apiKey;
28
    }
29
30
    public function getCookieName(): string
31
    {
32
        return $this->cookieName;
33
    }
34
}
35