Passed
Pull Request — master (#27)
by Anatoly
39:10
created

WWWAuthenticateHeader::getFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Header;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Message\Exception\InvalidHeaderValueException;
18
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException;
19
use Sunrise\Http\Message\Header;
20
21
/**
22
 * Import functions
23
 */
24
use function implode;
25
use function sprintf;
26
27
/**
28
 * @link https://tools.ietf.org/html/rfc7235#section-4.1
29
 */
30
class WWWAuthenticateHeader extends Header
31
{
32
33
    /**
34
     * HTTP Authentication Schemes
35
     *
36
     * @link https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
37
     */
38
    public const HTTP_AUTHENTICATE_SCHEME_BASIC         = 'Basic';
39
    public const HTTP_AUTHENTICATE_SCHEME_BEARER        = 'Bearer';
40
    public const HTTP_AUTHENTICATE_SCHEME_DIGEST        = 'Digest';
41
    public const HTTP_AUTHENTICATE_SCHEME_HOBA          = 'HOBA';
42
    public const HTTP_AUTHENTICATE_SCHEME_MUTUAL        = 'Mutual';
43
    public const HTTP_AUTHENTICATE_SCHEME_NEGOTIATE     = 'Negotiate';
44
    public const HTTP_AUTHENTICATE_SCHEME_OAUTH         = 'OAuth';
45
    public const HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_1   = 'SCRAM-SHA-1';
46
    public const HTTP_AUTHENTICATE_SCHEME_SCRAM_SHA_256 = 'SCRAM-SHA-256';
47
    public const HTTP_AUTHENTICATE_SCHEME_VAPID         = 'vapid';
48
49
    /**
50
     * @var string
51
     */
52
    private string $scheme;
53
54
    /**
55
     * @var array<string, string>
56
     */
57
    private array $parameters;
58
59
    /**
60
     * Constructor of the class
61
     *
62
     * @param string $scheme
63
     * @param array<array-key, mixed> $parameters
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
64
     *
65
     * @throws InvalidHeaderValueException
66
     *         If the scheme isn't valid.
67
     *
68
     * @throws InvalidHeaderValueParameterException
69
     *         If the parameters aren't valid.
70
     */
71 16
    public function __construct(string $scheme, array $parameters = [])
72
    {
73 16
        $this->validateToken($scheme);
74
75 14
        $parameters = $this->validateParameters($parameters);
76
77 10
        $this->scheme = $scheme;
78 10
        $this->parameters = $parameters;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 9
    public function getFieldName(): string
85
    {
86 9
        return 'WWW-Authenticate';
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 8
    public function getFieldValue(): string
93
    {
94 8
        $v = $this->scheme;
95
96 8
        $challenge = [];
97 8
        foreach ($this->parameters as $name => $value) {
98 7
            $challenge[] = sprintf(' %s="%s"', $name, $value);
99
        }
100
101 8
        if (!empty($challenge)) {
102 7
            $v .= implode(',', $challenge);
103
        }
104
105 8
        return $v;
106
    }
107
}
108