ApiToken   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 41
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 6 3
1
<?php
2
3
namespace DMT\Auth\Authorization;
4
5
use DMT\Auth\AuthorizationException;
6
use DMT\Auth\AuthorizationInterface;
7
use Psr\Http\Message\RequestInterface;
8
9
/**
10
 * Class ApiToken
11
 *
12
 * @package DMT\Auth
13
 */
14
class ApiToken implements AuthorizationInterface
15
{
16
    /**
17
     * @static string
18
     */
19
    const DEFAULT_API_KEY = 'X-API-Key';
20
21
    /**
22
     * @var string
23
     */
24
    protected $key = self::DEFAULT_API_KEY;
25
26
    /**
27
     * @var string
28
     */
29
    protected $token;
30
31
    /**
32
     * ApiToken constructor.
33
     *
34
     * @param string $key
35
     * @param string $token
36
     */
37 5
    public function __construct(string $token, string $key = self::DEFAULT_API_KEY)
38
    {
39 5
        $this->token = $token;
40 5
        $this->key = $key;
41 5
    }
42
43
    /**
44
     * Get a request with the headers associated with the authorization.
45
     *
46
     * @param RequestInterface $request
47
     * @return RequestInterface
48
     */
49 5
    public function handle(RequestInterface $request): RequestInterface
50
    {
51 5
        if ($this->token === '' || $this->key === '') {
52 4
            throw new AuthorizationException('Could not create api token, missing or empty arguments');
53
        }
54 1
        return $request->withHeader($this->key, $this->token);
55
    }
56
}
57