GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#19)
by
unknown
07:09
created

SoapManager   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 12.5%

Importance

Changes 7
Bugs 1 Features 4
Metric Value
wmc 26
c 7
b 1
f 4
lcom 1
cbo 1
dl 0
loc 221
rs 10
ccs 6
cts 48
cp 0.125

12 Methods

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 23 3
A extractResponseValueMatchingXPATH() 0 16 3
A extractResponseProperty() 0 4 1
A getException() 0 9 1
A setWSDL() 0 12 4
A setOptions() 0 10 3
A setHeaders() 0 8 2
A setOption() 0 4 3
A setNamespaces() 0 10 3
A setNamespace() 0 4 1
A getResponse() 0 4 1
A getRawResponse() 0 4 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Behat\SoapExtension\Utils;
6
7
/**
8
 * Trait SoapManager.
9
 *
10
 * @package Behat\SoapExtension\Utils
11
 */
12
trait SoapManager
13
{
14
    use ArrayManager;
15
16
    /**
17
     * URL of WSDL service to consume.
18
     *
19
     * @var string|null $wsdl
20
     */
21
    private $wsdl;
22
    /**
23
     * Set of options for SOAP request.
24
     *
25
     * @var array
26
     */
27
    private $options = [];
28
    /**
29
     * Response of SOAP method.
30
     *
31
     * @var mixed
32
     */
33
    private $response = [];
34
    /**
35
     * Last SOAP response.
36
     *
37
     * @var string
38
     */
39
    private $rawResponse = '';
40
    /**
41
     * The URIs of the namespaces.
42
     *
43
     * @var string[]
44
     */
45
    private $namespaces = [];
46
    /**
47
     * Latest exception thrown out during SOAP call.
48
     *
49
     * @var null|\SoapFault
50
     */
51
    private $exception;
52
53
    /**
54
     * Set of headers for SOAP request.
55
     *
56
     * @var array
57
     */
58
    private $headers = [];
59
60
    /**
61
     * Make SOAP call to a function with params.
62
     *
63
     * @link http://php.net/manual/en/soapclient.getlastrequest.php#example-5896
64
     *
65
     * @param string $function
66
     *   SOAP function name to execute. Use MethodNameIsIgnored if function name is in the XML body.
67
     * @param array $arguments
68
     *   Arguments array to pass to soap call function.
69
     */
70
    public function sendRequest($function, array $arguments)
71
    {
72
        // These values can be easily overridden inside of configuration file.
73
        $this->options += [
74
            // Important for raw response steps.
75
            'trace' => 1,
76
            'exceptions' => true,
77
            'cache_wsdl' => WSDL_CACHE_NONE,
78
        ];
79
80
        try {
81
            $client = new \SoapClient($this->wsdl, $this->options);
82
83
            if ($this->headers) {
84
                $client->__setSoapHeaders($this->headers);
85
            }
86
87
            $this->response = $client->__soapCall($function, $arguments);
88
            $this->rawResponse = $client->__getLastResponse();
89
        } catch (\SoapFault $e) {
90
            $this->exception = $e;
91
        }
92
    }
93
94
    /**
95
     * Extracts first value matching provided XPATH expression.
96
     *
97
     * @param string $query
98
     *   XPATH expression used to extract value from $this->rawResponse
99
     *
100
     * @return \DOMNode|bool
101
     */
102
    protected function extractResponseValueMatchingXPATH($query)
103
    {
104
        // @todo: Allow users to ignore namespaces via config or steps.
105
        // @example: $this->rawResponse = str_replace('xmlns=', 'ns=', $this->rawResponse);
106
        $dom = new \DOMDocument();
107
        $dom->loadXML($this->rawResponse);
108
        $xpath = new \DOMXpath($dom);
109
110
        foreach ($this->namespaces as $prefix => $uri) {
111 4
            $xpath->registerNamespace($prefix, $uri);
112
        }
113
114 4
        $nodeList = $xpath->query($query);
115
116 4
        return $nodeList->length > 0 ? $nodeList->item(0)->nodeValue : false;
117
    }
118
119 4
    /**
120 4
     * Helper to extract a property value from the response.
121
     *
122 4
     * @param string $property
123
     *
124
     * @return mixed
125
     */
126
    protected function extractResponseProperty($property)
127
    {
128
        return static::arrayValue(static::objectToArray($this->response), explode('][', $property));
129
    }
130
131
    /**
132
     * @return null|\SoapFault
133
     */
134
    protected function getException()
135
    {
136
        // When this method was called, this means thrown exception was read and won't be available anymore.
137
        $exception = $this->exception;
138
        // Reset the exception.
139
        $this->exception = null;
140
141
        return $exception;
142
    }
143
144
    /**
145
     * @param string $wsdl
146
     */
147
    protected function setWSDL($wsdl)
148
    {
149
        // Allow "null" and valid URLs.
150
        $isWsdlValid = null === $wsdl || filter_var($wsdl, FILTER_VALIDATE_URL);
151
        // Set the URL if it is validated.
152
        $this->wsdl = $isWsdlValid ? $wsdl : null;
153
154
        // Throw deferred exception when WSDL was reset due to it invalidation.
155
        if (!$isWsdlValid) {
156
            throw new \InvalidArgumentException(sprintf('You must pass a correct WSDL or null to %s.', __METHOD__));
157
        }
158
    }
159
160
    /**
161
     * @param array $options
162
     */
163
    protected function setOptions(array $options = null)
164
    {
165
        if (null === $options) {
166
            $this->options = [];
167
        } else {
168
            foreach ($options as $option => $value) {
169
                $this->setOption($option, $value);
170
            }
171
        }
172
    }
173
    /**
174
    * @param array $headers
175
    */
176
    protected function setHeaders(array $headers = null)
177
    {
178
        if (null === $headers) {
179
            $this->headers = [];
180
        } else {
181
            $this->headers = $headers;
182
        }
183
    }
184
185
    /**
186
     * @param string $option
187
     * @param mixed $value
188
     */
189
    protected function setOption($option, $value)
190
    {
191
        $this->options[$option] = is_string($value) && defined($value) ? constant($value) : $value;
192
    }
193
194
    /**
195
     * @param array $namespaces
196
     */
197
    protected function setNamespaces(array $namespaces = null)
198
    {
199
        if (null === $namespaces) {
200
            $this->namespaces = [];
201
        } else {
202
            foreach ($namespaces as $prefix => $uri) {
203
                $this->setNamespace($prefix, $uri);
204
            }
205
        }
206
    }
207
208
    /**
209
     * @param string $prefix
210
     * @param string $uri
211
     */
212
    protected function setNamespace($prefix, $uri)
213
    {
214
        $this->namespaces[$prefix] = $uri;
215
    }
216
217
    /**
218
     * @return mixed
219
     */
220
    public function getResponse()
221
    {
222
        return $this->response;
223
    }
224
225
    /**
226
     * @return string
227
     */
228
    public function getRawResponse()
229
    {
230
        return $this->rawResponse;
231
    }
232
}
233