WSCryptDecryptSoapClient::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the GestPayWS library.
5
 *
6
 * (c) Manuel Dalla Lana <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EndelWar\GestPayWS;
13
14
class WSCryptDecryptSoapClient
15
{
16
    protected $wsdlUrl = [
17
        'test' => 'https://sandbox.gestpay.net/gestpay/GestPayWS/WsCryptDecrypt.asmx?wsdl',
18
        'production' => 'https://ecomms2s.sella.it/gestpay/GestPayWS/WsCryptDecrypt.asmx?wsdl',
19
    ];
20
    public $wsdlEnvironment;
21
    protected $streamContextOption = [];
22
    protected $certificatePeerName = [
23
        'test' => 'sandbox.gestpay.net',
24
        'production' => 'ecomms2s.sella.it',
25
    ];
26
    /** @var \soapClient $soapClient */
27
    protected $soapClient;
28
    public $version = '1.4.0';
29
30
    /**
31
     * WSCryptDecryptSoapClient constructor.
32
     * @param bool|false $testEnv enable the test environment
33
     */
34
    public function __construct($testEnv = false)
35
    {
36
        $soapClientDefaultOption = [
37
            'user_agent' => 'EndelWar-GestPayWS/' . $this->version . ' (+https://github.com/endelwar/GestPayWS)',
38
            'stream_context' => $this->getStreamContext($testEnv),
39
            'connection_timeout' => 3000,
40
        ];
41
        if ($testEnv) {
42
            $soapClientEnvironmentOption = $this->setTestEnvironment();
43
        } else {
44
            $soapClientEnvironmentOption = $this->setProductionEnvironment();
45
        }
46
        $soapClientOption = array_merge($soapClientDefaultOption, $soapClientEnvironmentOption);
47
        $this->soapClient = new \soapClient($this->wsdlUrl[$this->wsdlEnvironment], $soapClientOption);
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    private function setTestEnvironment()
54
    {
55
        $this->wsdlEnvironment = 'test';
56
        $soapClientTestOption = [
57
            'trace' => true,
58
            'cache_wsdl' => WSDL_CACHE_NONE,
59
        ];
60
61
        return $soapClientTestOption;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    private function setProductionEnvironment()
68
    {
69
        $this->wsdlEnvironment = 'production';
70
71
        return [];
72
    }
73
74
    /**
75
     * @param bool $testEnv
76
     * @return resource
77
     */
78
    private function getStreamContext($testEnv = false)
79
    {
80
        if ($testEnv) {
81
            $host = $this->certificatePeerName['test'];
82
        } else {
83
            $host = $this->certificatePeerName['production'];
84
        }
85
86
        $this->streamContextOption['ssl']['crypto_method'] = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
87
88
        $this->streamContextOption['ssl']['allow_self_signed'] = true;
89
        $this->streamContextOption['ssl']['verify_peer'] = true;
90
        $this->streamContextOption['ssl']['SNI_enabled'] = true;
91
92
        // Disable TLS compression to prevent CRIME attacks.
93
        $this->streamContextOption['ssl']['disable_compression'] = true;
94
95
        $this->streamContextOption['ssl']['peer_name'] = $host;
96
        $this->streamContextOption['ssl']['verify_peer_name'] = true;
97
98
        return stream_context_create($this->streamContextOption);
99
    }
100
101
    /**
102
     * @return \soapClient
103
     */
104
    public function getSoapClient()
105
    {
106
        return $this->soapClient;
107
    }
108
}
109