Completed
Push — master ( b94d6d...7b872f )
by Sam
04:51
created

StreamContext::getResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Jalle19\CertificateParser\Provider;
4
5
/**
6
 * Class StreamContext
7
 * @package Jalle19\CertificateParser\Provider
8
 */
9
class StreamContext
10
{
11
12
    /**
13
     * @var bool
14
     */
15
    private $verifyPeerName;
16
17
    /**
18
     * @var string
19
     */
20
    private $sniServerName;
21
22
23
    /**
24
     * StreamContext constructor.
25
     *
26
     * @param bool        $verifyPeerName (optional)
27
     * @param string|null $sniServerName  (optional)
28
     */
29
    public function __construct($verifyPeerName = true, $sniServerName = null)
30
    {
31
        $this->verifyPeerName = $verifyPeerName;
32
        $this->sniServerName  = $sniServerName;
33
    }
34
35
36
    /**
37
     * @return boolean
38
     */
39
    public function verifyPeerName()
40
    {
41
        return $this->verifyPeerName;
42
    }
43
44
45
    /**
46
     * @return string
47
     */
48
    public function getSniServerName()
49
    {
50
        return $this->sniServerName;
51
    }
52
53
54
    /**
55
     * @return array
56
     */
57
    public function getArray()
58
    {
59
        $context = [
60
            'ssl' => [
61
                'capture_peer_cert' => true,
62
                'verify_peer'       => false,
63
                'verify_peer_name'  => $this->verifyPeerName(),
64
            ],
65
        ];
66
67
        if ($this->sniServerName !== null) {
68
            $context['ssl']['SNI_enabled']     = true;
69
            $context['ssl']['SNI_server_name'] = $this->getSniServerName();
70
        }
71
72
        return $context;
73
    }
74
75
76
    /**
77
     * @return resource
78
     */
79
    public function getResource()
80
    {
81
        return stream_context_create($this->getArray());
82
    }
83
}
84