Completed
Branch zenstruck-fork (6c313f)
by Kevin
03:51
created

ExpectedClaimValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
wmc 4
lcom 1
cbo 3
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 13 3
claim() 0 1 ?
1
<?php
2
3
namespace Zenstruck\JWT\Validator;
4
5
use Zenstruck\JWT\Exception\Validation\ClaimMismatch;
6
use Zenstruck\JWT\Exception\Validation\MissingClaim;
7
use Zenstruck\JWT\Token;
8
use Zenstruck\JWT\Validator;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
abstract class ExpectedClaimValidator implements Validator
14
{
15
    private $expected;
16
17
    /**
18
     * @param mixed $expected
19
     */
20 20
    public function __construct($expected)
21
    {
22 20
        $this->expected = $expected;
23 20
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 19
    public function validate(Token $token)
29
    {
30 19
        $claim = $this->claim();
31 19
        $actual = $token->get($claim);
32
33 19
        if (null === $actual) {
34 7
            throw new MissingClaim($claim, $token);
35
        }
36
37 12
        if ($this->expected !== $actual) {
38 5
            throw new ClaimMismatch($claim, $actual, $this->expected, $token);
39
        }
40 7
    }
41
42
    /**
43
     * @return string
44
     */
45
    abstract protected function claim();
46
}
47