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

CookieApiKeyAuthentication   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 25
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A readAuthorizationToken() 0 9 2
A getCookieName() 0 4 1
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