AudienceChecker   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 70
rs 10
c 0
b 0
f 0

7 Methods

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