Completed
Push — master ( 133544...35afa3 )
by Asmir
04:36
created

Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 13

Test Coverage

Coverage 90.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 0
cbo 13
dl 0
loc 119
ccs 49
cts 54
cp 0.9074
rs 10

4 Methods

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