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

CallableChecker::supportedHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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