Completed
Pull Request — master (#6)
by Asmir
124:43 queued 122:56
created

Client::__call()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 6.3078

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 35
cts 44
cp 0.7955
rs 8.6652
c 0
b 0
f 0
cc 6
eloc 44
nc 20
nop 2
crap 6.3078

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
namespace GoetasWebservices\SoapServices\SoapClient;
3
4
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReader;
5
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReaderInterface;
6
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler;
7
use GoetasWebservices\SoapServices\SoapClient\Exception\ClientException;
8
use GoetasWebservices\SoapServices\SoapClient\Exception\FaultException;
9
use GoetasWebservices\SoapServices\SoapClient\Exception\ServerException;
10
use GoetasWebservices\SoapServices\SoapClient\Exception\SoapException;
11
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreator;
12
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreatorInterface;
13
use GoetasWebservices\SoapServices\SoapClient\SoapEnvelope\Parts\Fault;
14
use GoetasWebservices\SoapServices\SoapEnvelope;
15
use Http\Client\Exception\HttpException;
16
use Http\Client\HttpClient;
17
use Http\Message\MessageFactory;
18
use JMS\Serializer\Serializer;
19
20
class Client
21
{
22
    /**
23
     * @var Serializer
24
     */
25
    protected $serializer;
26
    /**
27
     * @var array
28
     */
29
    protected $serviceDefinition;
30
    /**
31
     * @var HttpClient
32
     */
33
    protected $client;
34
35
    /**
36
     * @var MessageFactory
37
     */
38
    protected $messageFactory;
39
40
    /**
41
     * @var ResultCreatorInterface
42
     */
43
    private $resultCreator;
44
45
    /**
46
     * @var ArgumentsReaderInterface
47
     */
48
    private $argumentsReader;
49
50
51 30
    public function __construct(array $serviceDefinition, Serializer $serializer, MessageFactory $messageFactory, HttpClient $client, HeaderHandler $headerHandler)
52
    {
53 30
        $this->serviceDefinition = $serviceDefinition;
54 30
        $this->serializer = $serializer;
55
56 30
        $this->client = $client;
57 30
        $this->messageFactory = $messageFactory;
58 30
        $this->argumentsReader = new ArgumentsReader($this->serializer, $headerHandler);
59 30
        $this->resultCreator = new ResultCreator($this->serializer, !empty($serviceDefinition['unwrap']));
60 30
    }
61
62 28
    public function __call($functionName, array $args)
63
    {
64 28
        $soapOperation = $this->findOperation($functionName, $this->serviceDefinition);
65 28
        $message = $this->argumentsReader->readArguments($args, $soapOperation['input']);
66
67 28
        $xmlMessage = $this->serializer->serialize($message, 'xml');
0 ignored issues
show
Bug introduced by
It seems like $message defined by $this->argumentsReader->...soapOperation['input']) on line 65 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...
68 28
        $headers = $this->buildHeaders($soapOperation);
69 28
        $request = $this->messageFactory->createRequest('POST', $this->serviceDefinition['endpoint'], $headers, $xmlMessage);
70
71
        try {
72 28
            $response = $this->client->sendRequest($request);
73 18
            if (strpos($response->getHeaderLine('Content-Type'), 'xml') === false) {
74 2
                throw new ServerException(
75 2
                    $response,
76 2
                    $request,
77 2
                    "Unexpected content type '" . $response->getHeaderLine('Content-Type') . "'"
78 2
                );
79
            }
80
81
            // fast return if no return is expected
82 16
            if (!count($soapOperation['output']['parts'])) {
83 4
                return null;
84
            }
85
86 12
            $body = (string)$response->getBody();
87 12
            if (strpos($body, ':Fault>')!==false) {
88
                $fault = $this->serializer->deserialize($body, Fault::class, 'xml');
89
                throw new FaultException(
90
                    $fault,
91
                    $response,
92
                    $request,
93
                    "SOAP Fault",
94
                    null,
95
                    new \Exception()
96
                );
97
            }
98
99 12
            $response = $this->serializer->deserialize($body, $soapOperation['output']['message_fqcn'], 'xml');
100 24
        } catch (HttpException $e) {
101
102 10
            if (strpos($e->getResponse()->getHeaderLine('Content-Type'), 'xml') !== false) {
103 7
                $fault = $this->serializer->deserialize((string)$e->getResponse()->getBody(), Fault::class, 'xml');
104 6
                throw new FaultException(
105 6
                    $fault,
106 6
                    $e->getResponse(),
107 6
                    $e->getRequest(),
108 6
                    $e->getMessage(),
109 6
                    null,
110
                    $e
111 6
                );
112
            } else {
113 4
                throw new ServerException(
114 4
                    $e->getResponse(),
115 4
                    $e->getRequest(),
116 4
                    $e->getMessage(),
117 4
                    null,
118
                    $e
119 4
                );
120
            }
121
        }
122 12
        return $this->resultCreator->prepareResult($response, $soapOperation['output']);
123
    }
124
125 28
    protected function buildHeaders(array $operation)
126
    {
127
        return [
128 28
            'Content-Type' => isset($operation['version']) && $operation['version'] === '1.2'
129 28
                ? 'application/soap+xml; charset=utf-8'
130 28
                : 'text/xml; charset=utf-8',
131 28
            'SoapAction' => '"' . $operation['action'] . '"',
132 28
        ];
133
    }
134
135 28
    protected function findOperation($functionName, array $serviceDefinition)
136
    {
137 28
        if (isset($serviceDefinition['operations'][$functionName])) {
138 28
            return $serviceDefinition['operations'][$functionName];
139
        }
140
141
        foreach ($serviceDefinition['operations'] as $opName => $operation) {
142
            if (strtolower($functionName) == strtolower($opName)) {
143
                return $operation;
144
            }
145
        }
146
        throw new ClientException("Can not find an operation to run $functionName service call");
147
    }
148
}
149