Completed
Pull Request — master (#33)
by Robbert van den
03:06
created

Client::__call()   B

Complexity

Conditions 6
Paths 22

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 37
cts 37
cp 1
rs 8.3377
c 0
b 0
f 0
cc 6
nc 22
nop 2
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace GoetasWebservices\SoapServices\SoapClient;
4
5
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReader;
6
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReaderInterface;
7
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler;
8
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope\Messages\Fault as Fault11;
9
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope12\Messages\Fault as Fault12;
10
use GoetasWebservices\SoapServices\SoapClient\Exception\ClientException;
11
use GoetasWebservices\SoapServices\SoapClient\Exception\ServerException;
12
use GoetasWebservices\SoapServices\SoapClient\Exception\SoapException;
13
use GoetasWebservices\SoapServices\SoapClient\Exception\UnexpectedFormatException;
14
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreator;
15
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreatorInterface;
16
use GoetasWebservices\SoapServices\SoapEnvelope;
17
use Http\Client\Exception\HttpException;
18
use Http\Client\HttpClient;
19
use Http\Message\MessageFactory;
20
use JMS\Serializer\SerializationContext;
21
use JMS\Serializer\Serializer;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\RequestInterface;
24
25
class Client
26
{
27
    /**
28
     * @var Serializer
29
     */
30
    protected $serializer;
31
    /**
32
     * @var array
33
     */
34
    protected $serviceDefinition;
35
    /**
36
     * @var HttpClient
37
     */
38
    protected $client;
39
40
    /**
41
     * @var MessageFactory
42
     */
43
    protected $messageFactory;
44
45
    /**
46
     * @var ResultCreatorInterface
47
     */
48
    private $resultCreator;
49
50
    /**
51
     * @var ArgumentsReaderInterface
52
     */
53
    private $argumentsReader;
54
55
    /**
56
     * @var RequestInterface
57
     */
58
    private $requestMessage;
59
    /**
60
     * @var ResponseInterface
61
     */
62
    private $responseMessage;
63
64 36
    public function __construct(array $serviceDefinition, Serializer $serializer, MessageFactory $messageFactory, HttpClient $client, HeaderHandler $headerHandler)
65
    {
66 36
        $this->serviceDefinition = $serviceDefinition;
67 36
        $this->serializer = $serializer;
68
69 36
        $this->client = $client;
70 36
        $this->messageFactory = $messageFactory;
71 36
        $this->argumentsReader = new ArgumentsReader($this->serializer, $headerHandler);
72 36
        $this->resultCreator = new ResultCreator($this->serializer, !empty($serviceDefinition['unwrap']));
73 36
    }
74
75 34
    public function __call($functionName, array $args)
76
    {
77 34
        $soapOperation = $this->findOperation($functionName, $this->serviceDefinition);
78 34
        $message = $this->argumentsReader->readArguments($args, $soapOperation['input']);
79
80 34
        $xmlMessage = $this->serializer->serialize(
81 34
            $message,
0 ignored issues
show
Bug introduced by
It seems like $message defined by $this->argumentsReader->...soapOperation['input']) on line 78 can also be of type null; however, JMS\Serializer\Serializer::serialize() does only seem to accept object|array|integer|double|string|boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82 34
            'xml',
83 34
            (new SerializationContext())
84 34
                ->setAttribute('soapAction', $soapOperation['action'])
85 34
                ->setAttribute('soapEndpoint', $this->serviceDefinition['endpoint'])
86 34
        );
87
88 34
        $headers = $this->buildHeaders($soapOperation);
89 34
        $this->requestMessage = $request = $this->messageFactory->createRequest('POST', $this->serviceDefinition['endpoint'], $headers, $xmlMessage);
90
91
        try {
92 34
            $this->responseMessage = $response = $this->client->sendRequest($request);
93 24
            if (strpos($response->getHeaderLine('Content-Type'), 'xml') === false) {
94 2
                throw new UnexpectedFormatException(
95 2
                    $response,
96 2
                    $request,
97 2
                    "Unexpected content type '" . $response->getHeaderLine('Content-Type') . "'"
98 2
                );
99
            }
100
101
            // fast return if no return is expected
102 22
            if (!count($soapOperation['output']['parts'])) {
103 4
                return null;
104
            }
105
106 18
            $body = (string)$response->getBody();
107
108 18
            $faultClass = $this->findFaultClass($response);
109
110 18
            if (strpos($body, ':Fault>') !== false) { // some server returns a fault with 200 OK HTTP
111 1
                $fault = $this->serializer->deserialize($body, $faultClass, 'xml');
112 1
                throw $fault->createException($response, $request);
113
            }
114
115 17
            $response = $this->serializer->deserialize($body, $soapOperation['output']['message_fqcn'], 'xml');
116 30
        } catch (HttpException $e) {
117 10
            if (strpos($e->getResponse()->getHeaderLine('Content-Type'), 'xml') !== false) {
118 6
                $faultClass = $this->findFaultClass($e->getResponse());
119 6
                $fault = $this->serializer->deserialize((string)$e->getResponse()->getBody(), $faultClass, 'xml');
120 6
                throw $fault->createException($e->getResponse(), $e->getRequest(), $e);
121
            } else {
122 4
                throw new ServerException(
123 4
                    $e->getResponse(),
124 4
                    $e->getRequest(),
125
                    $e
126 4
                );
127
            }
128
        }
129 17
        return $this->resultCreator->prepareResult($response, $soapOperation['output']);
130
    }
131
132
    /**
133
     * @return RequestInterface|null
134
     */
135 2
    public function __getLastRequestMessage()
136
    {
137 2
        return $this->requestMessage;
138
    }
139
    /**
140
     * @return ResponseInterface|null
141
     */
142 2
    public function __getLastResponseMessage()
143
    {
144 2
        return $this->responseMessage;
145
    }
146
147 24
    protected function findFaultClass(ResponseInterface $response)
148
    {
149 24
        if (strpos($response->getHeaderLine('Content-Type'), 'application/soap+xml') !== false) {
150 13
            return Fault12::class;
151
        } else {
152 11
            return Fault11::class;
153
        }
154
    }
155
156 34
    protected function buildHeaders(array $operation)
157
    {
158
        return [
159 34
            'Content-Type' => isset($operation['version']) && $operation['version'] === '1.2'
160 34
                ? 'application/soap+xml; charset=utf-8'
161 34
                : 'text/xml; charset=utf-8',
162 34
            'SoapAction' => '"' . $operation['action'] . '"',
163 34
        ];
164
    }
165
166 34
    protected function findOperation($functionName, array $serviceDefinition)
167
    {
168 34
        if (isset($serviceDefinition['operations'][$functionName])) {
169 33
            return $serviceDefinition['operations'][$functionName];
170
        }
171
172 1
        foreach ($serviceDefinition['operations'] as $operation) {
173 1
            if (strtolower($functionName) == strtolower($operation['method'])) {
174 1
                return $operation;
175
            }
176
        }
177
        throw new ClientException("Can not find an operation to run $functionName service call");
178
    }
179
}
180