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 (#8)
by Alexei
06:32 queued 04:24
created

SoapManager::setException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    /**
48
     * @var \SoapFault|null
49
     */
50
    private $exception;
51
52
    /**
53
     * Make SOAP call to a function with params.
54
     *
55
     * @param string $function
56
     *   SOAP function name to execute. Use MethodNameIsIgnored if function name is in the XML body.
57
     * @param array $arguments
58
     *   Arguments array to pass to soap call function.
59
     */
60
    protected function sendRequest($function, array $arguments)
61
    {
62
        // These values can be easily overridden inside of configuration file.
63
        $this->options += [
64
            // Important for raw response steps.
65
            'trace' => 1,
66
            'exceptions' => true,
67
            'cache_wsdl' => WSDL_CACHE_NONE,
68
        ];
69
70
        $client = new \SoapClient($this->wsdl, $this->options);
71
72
        try {
73
            $this->response = $client->__soapCall($function, $arguments);
74
            $this->rawResponse = $client->__getLastResponse();
75
        } catch (\SoapFault $e) {
76
            $this->exception = $e;
77
        }
78
    }
79
80
    /**
81
     * Extracts first value matching provided XPATH expression.
82
     *
83
     * @param string $query
84
     *   XPATH expression used to extract value from $this->rawResponse
85
     *
86
     * @return \DOMNode|bool
87
     */
88
    protected function extractResponseValueMatchingXPATH($query)
89
    {
90
        // @todo: Allow users to ignore namespaces via config or steps.
91
        // @example: $this->rawResponse = str_replace('xmlns=', 'ns=', $this->rawResponse);
92
        $dom = new \DOMDocument();
93
        $dom->loadXML($this->rawResponse);
94
        $xpath = new \DOMXpath($dom);
95
96
        foreach ($this->namespaces as $prefix => $uri) {
97
            $xpath->registerNamespace($prefix, $uri);
98
        }
99
100
        $nodeList = $xpath->query($query);
101
102
        return $nodeList->length > 0 ? $nodeList->item(0)->nodeValue : false;
103
    }
104
105
    /**
106
     * Helper to extract a property value from the response.
107
     *
108
     * @param string $property
109
     *
110
     * @return mixed
111 4
     */
112
    protected function extractResponseProperty($property)
113
    {
114 4
        return static::arrayValue(static::objectToArray($this->response), explode('][', $property));
115
    }
116 4
117
    /**
118
     * @param string $wsdl
119 4
     */
120 4
    protected function setWSDL($wsdl)
121
    {
122 4
        // Allow "null" and valid URLs.
123
        $isWsdlValid = null === $wsdl || filter_var($wsdl, FILTER_VALIDATE_URL);
124
        // Set the URL if it is validated.
125
        $this->wsdl = $isWsdlValid ? $wsdl : null;
126
127
        // Throw deferred exception when WSDL was reset due to it invalidation.
128
        if (!$isWsdlValid) {
129
            throw new \InvalidArgumentException(sprintf('You must pass a correct WSDL or null to %s.', __METHOD__));
130
        }
131
    }
132
133
    /**
134
     * @param array $options
135
     */
136
    protected function setOptions(array $options = null)
137
    {
138
        if (null === $options) {
139
            $this->options = [];
140
        } else {
141
            foreach ($options as $option => $value) {
142
                $this->setOption($option, $value);
143
            }
144
        }
145
    }
146
147
    /**
148
     * @param string $option
149
     * @param mixed $value
150
     */
151
    protected function setOption($option, $value)
152
    {
153
        $this->options[$option] = defined($value) ? constant($value) : $value;
154
    }
155
156
    /**
157
     * @param array $namespaces
158
     */
159
    protected function setNamespaces(array $namespaces = null)
160
    {
161
        if (null === $namespaces) {
162
            $this->namespaces = [];
163
        } else {
164
            foreach ($namespaces as $prefix => $uri) {
165
                $this->setNamespace($prefix, $uri);
166
            }
167
        }
168
    }
169
170
    /**
171
     * @param string $prefix
172
     * @param string $uri
173
     */
174
    protected function setNamespace($prefix, $uri)
175
    {
176
        $this->namespaces[$prefix] = $uri;
177
    }
178
179
    /**
180
     * @return \SoapFault
181
     */
182
    public function getException()
183
    {
184
        return $this->exception;
185
    }
186
187
    /**
188
     * @param \SoapFault $exception
189
     */
190
    public function setException(\SoapFault $exception = null)
191
    {
192
        $this->exception = $exception;
193
    }
194
}
195