Completed
Push — master ( 6cb479...b909d5 )
by Meng
02:30
created

RequestBuilder::isSOAP12()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Meng\Soap\HttpBinding;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\StreamInterface;
7
use Zend\Diactoros\Request;
8
use Zend\Diactoros\Stream;
9
10
/**
11
 * This class create PSR HTTP requests that embed SOAP messages.
12
 */
13
class RequestBuilder
14
{
15
    const SOAP11 = '1.1';
16
    const SOAP12 = '1.2';
17
18
    /**
19
     * @var string
20
     */
21
    private $endpoint;
22
    /**
23
     * @var string
24
     */
25
    private $soapVersion = self::SOAP11;
26
    /**
27
     * @var string
28
     */
29
    private $soapAction = '';
30
    /**
31
     * @var StreamInterface
32
     */
33
    private $soapMessage;
34
    /**
35
     * @var string
36
     */
37
    private $httpMethod = 'POST';
38
39
    /**
40
     * @return RequestInterface
41
     */
42
    public function getSoapHttpRequest()
43
    {
44
        $this->validate();
45
        $headers = $this->prepareHeaders();
46
        $message = $this->prepareMessage();
47
        return new Request(
48
            $this->endpoint,
49
            $this->httpMethod,
50
            $message,
51
            $headers
52
        );
53
    }
54
55
    /**
56
     * @param string $endpoint
57
     * @return self
58
     */
59
    public function setEndpoint($endpoint)
60
    {
61
        $this->endpoint = $endpoint;
62
        return $this;
63
    }
64
65
    /**
66
     * @return self
67
     */
68
    public function isSOAP11()
69
    {
70
        $this->soapVersion = self::SOAP11;
71
        return $this;
72
    }
73
74
    public function isSOAP12()
75
    {
76
        $this->soapVersion = self::SOAP12;
77
        return $this;
78
    }
79
80
81
    /**
82
     * @param string $soapAction
83
     * @return self
84
     */
85
    public function setSoapAction($soapAction)
86
    {
87
        $this->soapAction = $soapAction;
88
        return $this;
89
    }
90
91
    /**
92
     * @param StreamInterface $message
93
     * @return self
94
     */
95
    public function setSoapMessage($message)
96
    {
97
        $this->soapMessage = $message;
98
        return $this;
99
    }
100
101
    /**
102
     * @param string $method
103
     * @return self
104
     */
105
    public function setHttpMethod($method)
106
    {
107
        $this->httpMethod = $method;
108
        return $this;
109
    }
110
111
    private function validate()
112
    {
113
        $isValid = true;
114
115
        if (!$this->endpoint) {
116
            $isValid = false;
117
        }
118
119
        if (!$this->soapMessage && $this->httpMethod != 'GET') {
120
            $isValid = false;
121
        }
122
123
        /**
124
         * SOAP 1.1 only defines HTTP binding with POST method.
125
         * @link https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383527
126
         */
127
        if ($this->soapVersion == self::SOAP11 && $this->httpMethod != 'POST') {
128
            $isValid = false;
129
        }
130
131
        /**
132
         * SOAP 1.2 only defines HTTP binding with POST and GET methods.
133
         * @link https://www.w3.org/TR/2007/REC-soap12-part0-20070427/#L10309
134
         */
135
        if ($this->soapVersion == self::SOAP12 && !in_array($this->httpMethod, ['GET', 'POST'])) {
136
            $isValid = false;
137
        }
138
139
        if (!$isValid) {
140
            throw new RequestException;
141
        }
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    private function prepareHeaders()
148
    {
149
        if ($this->soapVersion == self::SOAP11) {
150
            return $this->prepareSoap11Headers();
151
        } else {
152
            return $this->prepareSoap12Headers();
153
        }
154
    }
155
156
    /**
157
     * @link https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383526
158
     * @return array
159
     */
160
    private function prepareSoap11Headers()
161
    {
162
        $headers = [];
163
        $headers['Content-Length'] = strlen($this->soapMessage->getContents());
164
        $headers['SOAPAction'] = $this->soapAction;
165
        $headers['Content-Type'] = 'text/xml; charset="utf-8"';
166
        return $headers;
167
    }
168
169
    /**
170
     * SOSPAction header is removed in SOAP 1.2 and now expressed as a value of
171
     * an (optional) "action" parameter of the "application/soap+xml" media type.
172
     * @link https://www.w3.org/TR/soap12-part0/#L4697
173
     * @return array
174
     */
175
    private function prepareSoap12Headers()
176
    {
177
        $headers = [];
178
        if ($this->httpMethod == 'POST') {
179
            $headers['Content-Length'] = strlen($this->soapMessage->getContents());
180
            $headers['Content-Type'] = 'application/soap+xml; charset="utf-8"' . '; action="' . $this->soapAction . '"';
181
        } else {
182
            $headers['Accept'] = 'application/soap+xml';
183
        }
184
        return $headers;
185
    }
186
187
    /**
188
     * @return StreamInterface
189
     */
190
    private function prepareMessage()
191
    {
192
        if ($this->httpMethod == 'POST') {
193
            return $this->soapMessage;
194
        } else {
195
            return new Stream(fopen('php://temp', 'r'));
196
        }
197
    }
198
}