Completed
Push — master ( 03622e...118594 )
by Meng
03:10
created

Factory::isHttpUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Meng\AsyncSoap\Guzzle;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Promise\FulfilledPromise;
7
use Meng\AsyncSoap\SoapClientInterface;
8
use Meng\Soap\HttpBinding\HttpBinding;
9
use Meng\Soap\HttpBinding\RequestBuilder;
10
use Meng\Soap\Interpreter;
11
use Psr\Http\Message\ResponseInterface;
12
13
class Factory
14
{
15
    /**
16
     * Create an instance of SoapClientInterface.
17
     *
18
     * This method will load WSDL asynchronously if the given WSDL URI is a HTTP URL.
19
     *
20
     * @param ClientInterface $client       A Guzzle HTTP client.
21
     * @param mixed $wsdl                   URI of the WSDL file or NULL if working in non-WSDL mode.
22
     * @param array $options                Supported options: location, uri, style, use, soap_version, encoding,
23
     *                                      exceptions, classmap, typemap, and feature. HTTP related options should
24
     *                                      be configured against $client, e.g., authentication, proxy, user agent,
25
     *                                      and connection timeout etc.
26
     * @return SoapClientInterface
27
     */
28
    public function create(ClientInterface $client, $wsdl, array $options = [])
29
    {
30
        if ($this->isHttpUrl($wsdl)) {
31
            $httpBindingPromise = $client->requestAsync('GET', $wsdl)->then(
32
                function (ResponseInterface $response) use ($options) {
33
                    $wsdl = $response->getBody()->__toString();
34
                    $interpreter = new Interpreter('data://text/plain;base64,' . base64_encode($wsdl), $options);
35
                    return new HttpBinding($interpreter, new RequestBuilder);
36
                }
37
            );
38
        } else {
39
            $httpBindingPromise = new FulfilledPromise(
40
                new HttpBinding(new Interpreter($wsdl, $options), new RequestBuilder)
41
            );
42
        }
43
44
        return new SoapClient($client, $httpBindingPromise);
45
    }
46
47
    private function isHttpUrl($wsdl)
48
    {
49
        return filter_var($wsdl, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) !== false
50
            && in_array(parse_url($wsdl, PHP_URL_SCHEME), ['http', 'https']);
51
    }
52
}