Failed Conditions
Push — v7 ( ae6905...7dd7be )
by Florent
03:50
created

Recipient::getHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 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\Encryption;
15
16
/**
17
 * Class Recipient
18
 */
19
final class Recipient
20
{
21
    /**
22
     * @var array
23
     */
24
    private $headers = [];
25
26
    /**
27
     * @var null|string
28
     */
29
    private $encryptedKey = null;
30
31
    /**
32
     * Recipient constructor.
33
     *
34
     * @param array $headers
35
     * @param null|string $encryptedKey
36
     */
37
    private function __construct(array $headers, ?string $encryptedKey)
38
    {
39
        $this->headers = $headers;
40
        $this->encryptedKey = $encryptedKey;
41
    }
42
43
    /**
44
     * @param array       $headers
45
     * @param null|string $encryptedKey
46
     *
47
     * @return Recipient
48
     */
49
    public static function create(array $headers = [], ?string $encryptedKey):Recipient
50
    {
51
        return new self($headers, $encryptedKey);
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getHeaders(): array
58
    {
59
        return $this->headers;
60
    }
61
62
    /**
63
     * Returns the value of the unprotected header of the specified key.
64
     *
65
     * @param string $key The key
66
     *
67
     * @return mixed|null Header value
68
     */
69
    public function getHeader(string $key)
70
    {
71
        if ($this->hasHeader($key)) {
72
            return $this->headers[$key];
73
        }
74
        throw new \InvalidArgumentException(sprintf('The header "%s" does not exist.', $key));
75
    }
76
77
    /**
78
     * @param string $key The key
79
     *
80
     * @return bool
81
     */
82
    public function hasHeader(string $key): bool
83
    {
84
        return array_key_exists($key, $this->headers);
85
    }
86
87
    /**
88
     * @return null|string
89
     */
90
    public function getEncryptedKey(): ?string
91
    {
92
        return $this->encryptedKey;
93
    }
94
}
95