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

ContentEncryptionAlgorithmChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkHeader() 0 9 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-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\HeaderChecker;
17
use Jose\Component\Checker\InvalidHeaderException;
18
19
/**
20
 * This class is a header parameter checker.
21
 * When the "enc" header parameter is present, it will check if the value is within the allowed ones.
22
 */
23
final class ContentEncryptionAlgorithmChecker implements HeaderChecker
24
{
25
    private const HEADER_NAME = 'enc';
26
27
    /**
28
     * @var bool
29
     */
30
    private $protectedHeader = false;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $supportedAlgorithms;
36
37
    /**
38
     * @param string[] $supportedAlgorithms
39
     */
40
    public function __construct(array $supportedAlgorithms, bool $protectedHeader = false)
41
    {
42
        $this->supportedAlgorithms = $supportedAlgorithms;
43
        $this->protectedHeader = $protectedHeader;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws InvalidHeaderException if the header is invalid
50
     */
51
    public function checkHeader($value): void
52
    {
53
        if (!\is_string($value)) {
54
            throw new InvalidHeaderException('"enc" must be a string.', self::HEADER_NAME, $value);
55
        }
56
        if (!\in_array($value, $this->supportedAlgorithms, true)) {
57
            throw new InvalidHeaderException('Unsupported algorithm.', self::HEADER_NAME, $value);
58
        }
59
    }
60
61
    public function supportedHeader(): string
62
    {
63
        return self::HEADER_NAME;
64
    }
65
66
    public function protectedHeaderOnly(): bool
67
    {
68
        return $this->protectedHeader;
69
    }
70
}
71