Failed Conditions
Push — master ( 71e756...c4ef0e )
by Florent
08:21 queued 10s
created

CallableChecker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 59
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkClaim() 0 8 2
A supportedClaim() 0 4 1
A checkHeader() 0 8 2
A supportedHeader() 0 4 1
A protectedHeaderOnly() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Easy;
15
16
use Jose\Component\Checker\ClaimChecker;
17
use Jose\Component\Checker\HeaderChecker;
18
use Jose\Component\Checker\InvalidClaimException;
19
use Jose\Component\Checker\InvalidHeaderException;
20
21
final class CallableChecker implements ClaimChecker, HeaderChecker
22
{
23
    /**
24
     * @var string
25
     */
26
    private $key;
27
28
    /**
29
     * @var callable
30
     */
31
    private $callable;
32
33
    public function __construct(string $key, callable $callable)
34
    {
35
        $this->key = $key;
36
        $this->callable = $callable;
37
    }
38
39
    /**
40
     * @param mixed $value
41
     *
42
     * @throws InvalidClaimException if the claim is invalid
43
     */
44
    public function checkClaim($value): void
45
    {
46
        $callable = $this->callable;
47
        $isValid = $callable($value);
48
        if (!$isValid) {
49
            throw new InvalidClaimException(sprintf('Invalid claim "%s"', $this->key), $this->key, $value);
50
        }
51
    }
52
53
    public function supportedClaim(): string
54
    {
55
        return $this->key;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function checkHeader($value): void
62
    {
63
        $callable = $this->callable;
64
        $isValid = $callable($value);
65
        if (!$isValid) {
66
            throw new InvalidHeaderException(sprintf('Invalid header "%s"', $this->key), $this->key, $value);
67
        }
68
    }
69
70
    public function supportedHeader(): string
71
    {
72
        return $this->key;
73
    }
74
75
    public function protectedHeaderOnly(): bool
76
    {
77
        return true;
78
    }
79
}
80