AbstractHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 32
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setSecretKey() 0 3 1
A calculateSignature() 0 8 1
1
<?php
2
/**
3
 * File: AbstractHandler.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Jwt\Handler;
10
11
/**
12
 * Class AbstractHandler
13
 *
14
 * @package MSlwk\Jwt\Handler
15
 */
16
class AbstractHandler
17
{
18
    const HASHING_ALGORITHM = 'sha256';
19
20
    const ENCODING_ALGORITHM = 'HS256';
21
    /**
22
     * @var string
23
     */
24
    protected $secretKey = 'notThatSecretReally-changeMe';
25
26
    /**
27
     * @param string $secretKey
28
     * @return null
29
     */
30 8
    public function setSecretKey(string $secretKey)
31
    {
32 8
        $this->secretKey = $secretKey;
33 8
    }
34
35
    /**
36
     * @param string $header
37
     * @param string $payload
38
     * @return string
39
     */
40 7
    protected function calculateSignature(string $header, string $payload): string
41
    {
42 7
        return base64_encode(
43 7
            hash_hmac(
44 7
                self::HASHING_ALGORITHM,
45 7
                "{$header}.{$payload}",
46 7
                $this->secretKey,
47 7
                true
48
            )
49
        );
50
    }
51
}
52