Failed Conditions
Push — v7 ( 73ebba...e5c6da )
by Florent
02:18
created

AlgorithmChecker::supportedHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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
 * Class AlgorithmChecker
18
 */
19
final class AlgorithmChecker implements HeaderCheckerInterface
20
{
21
    private const HEADER_NAME = 'alg';
22
23
    /**
24
     * @var bool
25
     */
26
    private $protectedHeader = false;
27
28
    /**
29
     * @var string[]
30
     */
31
    private $supportedAlgorithms;
32
33
    /**
34
     * AudienceChecker constructor.
35
     *
36
     * @param string[] $supportedAlgorithms
37
     * @param bool     $protectedHeader
38
     */
39
    public function __construct(array $supportedAlgorithms, bool $protectedHeader = false)
40
    {
41
        $this->supportedAlgorithms = $supportedAlgorithms;
42
        $this->protectedHeader = $protectedHeader;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function checkHeader($value)
49
    {
50
        if (is_string($value) && !in_array($value, $this->supportedAlgorithms)) {
51
            throw new \InvalidArgumentException('Unsupported algorithm.');
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportedHeader(): string
59
    {
60
        return self::HEADER_NAME;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function protectedHeaderOnly(): bool
67
    {
68
        return $this->protectedHeader;
69
    }
70
}
71