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

AlgorithmChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkHeader() 0 6 3
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-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