IssuerChecker   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 71
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkClaim() 0 4 1
A checkHeader() 0 4 1
A supportedClaim() 0 4 1
A supportedHeader() 0 4 1
A protectedHeaderOnly() 0 4 1
A checkValue() 0 9 3
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\Component\Checker;
15
16
/**
17
 * This class is a header parameter and claim checker.
18
 * When the "iss" header parameter or claim is present, it will check if the value is within the allowed ones.
19
 */
20
final class IssuerChecker implements ClaimChecker, HeaderChecker
21
{
22
    private const CLAIM_NAME = 'iss';
23
24
    /**
25
     * @var bool
26
     */
27
    private $protectedHeader = false;
28
29
    /**
30
     * @var array
31
     */
32
    private $issuers;
33
34
    public function __construct(array $issuer, bool $protectedHeader = false)
35
    {
36
        $this->issuers = $issuer;
37
        $this->protectedHeader = $protectedHeader;
38
    }
39
40
    /**
41
     * @param mixed $value
42
     *
43
     * @throws InvalidClaimException if the claim is invalid
44
     */
45
    public function checkClaim($value): void
46
    {
47
        $this->checkValue($value, InvalidClaimException::class);
48
    }
49
50
    /**
51
     * @param mixed $value
52
     *
53
     * @throws InvalidHeaderException if the header parameter is invalid
54
     */
55
    public function checkHeader($value): void
56
    {
57
        $this->checkValue($value, InvalidHeaderException::class);
58
    }
59
60
    public function supportedClaim(): string
61
    {
62
        return self::CLAIM_NAME;
63
    }
64
65
    public function supportedHeader(): string
66
    {
67
        return self::CLAIM_NAME;
68
    }
69
70
    public function protectedHeaderOnly(): bool
71
    {
72
        return $this->protectedHeader;
73
    }
74
75
    /**
76
     * @param mixed $value
77
     *
78
     * @throws InvalidClaimException  if the claim is invalid
79
     * @throws InvalidHeaderException if the header parameter is invalid
80
     */
81
    private function checkValue($value, string $class): void
82
    {
83
        if (!\is_string($value)) {
84
            throw new $class('Invalid value.', self::CLAIM_NAME, $value);
85
        }
86
        if (!\in_array($value, $this->issuers, true)) {
87
            throw new $class('Unknown issuer.', self::CLAIM_NAME, $value);
88
        }
89
    }
90
}
91