ApiToken::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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