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

StreamContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A verifyPeerName() 0 4 1
A getSniServerName() 0 4 1
A getArray() 0 17 2
A getResource() 0 4 1
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