SslOptions::toStreamContext()   B
last analyzed

Complexity

Conditions 7
Paths 28

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 28
nop 1
dl 0
loc 26
ccs 12
cts 12
cp 1
crap 7
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf\Transport;
14
15
/**
16
 * Abstraction of supported SSL configuration parameters
17
 *
18
 * @author Benjamin Zikarsky <[email protected]>
19
 */
20
class SslOptions
21
{
22
    /**
23
     * Enable certificate validation of remote party
24
     */
25
    private bool $verifyPeer = true;
26
27
    /**
28
     * Allow self-signed certificates
29
     */
30
    private bool $allowSelfSigned = false;
31
32
    /**
33
     * Require verification of peer name.
34
     */
35
    private bool $verifyPeerName = true;
36
37
    /**
38
     * Path to custom CA
39
     */
40
    private ?string $caFile = null;
41
42
    /**
43
     * List of ciphers the SSL layer may use
44
     *
45
     * Formatted as specified in `ciphers(1)`
46
     */
47
    private ?string $ciphers = null;
48
49
    /**
50
     * Whether self-signed certificates are allowed
51
     */
52
    public function getAllowSelfSigned(): bool
53
    {
54
        return $this->allowSelfSigned;
55
    }
56 1
57
    /**
58 1
     * Enables or disables the error on self-signed certificates
59
     */
60
    public function setAllowSelfSigned(bool $allowSelfSigned): void
61
    {
62
        $this->allowSelfSigned = $allowSelfSigned;
63
    }
64
65
    /**
66 2
     * Returns the path to a custom CA
67
     */
68 2
    public function getCaFile(): ?string
69 2
    {
70
        return $this->caFile;
71
    }
72
73
    /**
74
     * Sets the path toa custom CA
75
     */
76 1
    public function setCaFile(?string $caFile): void
77
    {
78 1
        $this->caFile = $caFile;
79
    }
80
81
    /**
82
     * Returns des description of allowed ciphers
83
     */
84
    public function getCiphers(): ?string
85
    {
86 2
        return $this->ciphers;
87
    }
88 2
89 2
    /**
90
     * Set the allowed SSL/TLS ciphers
91
     *
92
     * Format must follow `ciphers(1)`
93
     */
94
    public function setCiphers(?string $ciphers): void
95
    {
96 1
        $this->ciphers = $ciphers;
97
    }
98 1
99
    /**
100
     * Whether to check the peer certificate
101
     */
102
    public function getVerifyPeer(): bool
103
    {
104
        return $this->verifyPeer;
105
    }
106
107
    /**
108 2
     * Enable or disable the peer certificate check
109
     */
110 2
    public function setVerifyPeer(bool $verifyPeer): void
111 2
    {
112
        $this->verifyPeer = $verifyPeer;
113
    }
114
115
    /**
116
     * Whether to check the peer name
117
     */
118 1
    public function getVerifyPeerName(): bool
119
    {
120 1
        return $this->verifyPeerName;
121
    }
122
123
    /**
124
     * Enable or disable the peer name check
125
     */
126
    public function setVerifyPeerName(bool $verifyPeerName): void
127
    {
128 4
        $this->verifyPeerName = $verifyPeerName;
129
    }
130 4
131 4
    /**
132
     * Returns a stream-context representation of this config
133
     */
134
    public function toStreamContext(?string $serverName = null): array
135
    {
136
        $sslContext = [
137
            'verify_peer'       => $this->verifyPeer,
138
            'verify_peer_name'  => $this->verifyPeerName,
139 5
            'allow_self_signed' => $this->allowSelfSigned
140
        ];
141
142 5
        if (null !== $this->caFile) {
143 5
            $sslContext['cafile'] = $this->caFile;
144
        }
145
146 5
        if (null !== $this->ciphers) {
147 1
            $sslContext['ciphers'] = $this->ciphers;
148
        }
149
150 5
        if (null !== $serverName) {
151 1
            $sslContext['SNI_enabled'] = true;
152
            $sslContext[PHP_VERSION_ID < 50600 ? 'SNI_server_name' : 'peer_name'] = $serverName;
153
154 5
            if ($this->verifyPeer) {
155 4
                $sslContext[PHP_VERSION_ID < 50600 ? 'CN_match' : 'peer_name'] = $serverName;
156 4
            }
157
        }
158 4
159 4
        return ['ssl' => $sslContext];
160
    }
161
}
162