Completed
Push — master ( a0e93f...c87c59 )
by Benjamin
01:59
created

SslOptions::toStreamContext()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

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