Completed
Push — master ( 251d07...973a55 )
by Oleksandr
10s
created

AbstractCall   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 164
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 16 1
executeCall() 0 1 ?
throwValidationException() 0 1 ?
A sendRequest() 0 12 1
A createSoapClient() 0 12 1
A validateResponse() 0 7 2
A extractExceptionMessage() 0 10 3
A retrieveOrderNumber() 0 8 2
A getRequestOptions() 0 6 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\ArvatoRss\Business\Api\Adapter\ApiCall;
9
10
use Generated\Shared\Transfer\ArvatoRssIdentificationRequestTransfer;
11
use SoapClient;
12
use SoapFault;
13
use Spryker\Shared\Config\Config;
14
use SprykerEco\Shared\ArvatoRss\ArvatoRssConstants;
15
use SprykerEco\Zed\ArvatoRss\Business\Api\Adapter\ApiCall\Logger\ApiCallLoggerInterface;
16
use SprykerEco\Zed\ArvatoRss\Business\Api\ArvatoRssRequestApiConfig;
17
use SprykerEco\Zed\ArvatoRss\Business\Api\Converter\RequestHeaderConverterInterface;
18
19
abstract class AbstractCall implements ApiCallInterface
20
{
21
    /**
22
     * @const string
23
     */
24
    const WSDL_PATH = __DIR__ . "/../../Etc/risk-solution-services.v2.1.wsdl";
25
26
    /**
27
     * @const string
28
     */
29
    const CALL_TYPE = 'UNKNOWN';
30
31
    /**
32
     * @var \SprykerEco\Zed\ArvatoRss\Business\Api\Converter\RequestHeaderConverterInterface
33
     */
34
    protected $requestHeaderConverter;
35
36
    /**
37
     * @var \SprykerEco\Zed\ArvatoRss\Business\Api\Adapter\ApiCall\Logger\ApiCallLoggerInterface
38
     */
39
    protected $apiCallLogger;
40
41
    /**
42
     * @param \SprykerEco\Zed\ArvatoRss\Business\Api\Converter\RequestHeaderConverterInterface $requestHeaderConverter
43
     * @param \SprykerEco\Zed\ArvatoRss\Business\Api\Adapter\ApiCall\Logger\ApiCallLoggerInterface $apiCallLogger
44
     */
45
    public function __construct(
46
        RequestHeaderConverterInterface $requestHeaderConverter,
47
        ApiCallLoggerInterface $apiCallLogger
48
    ) {
49
        $this->requestHeaderConverter = $requestHeaderConverter;
50
        $this->apiCallLogger = $apiCallLogger;
51
    }
52
53
    /**
54
     * @param \Generated\Shared\Transfer\ArvatoRssIdentificationRequestTransfer $identification
55
     * @param array $params
56
     *
57
     * @return \stdClass
58
     */
59
    public function execute(
60
        ArvatoRssIdentificationRequestTransfer $identification,
61
        array $params
62
    ) {
63
        $result = $this->sendRequest($identification, $params);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->sendRequest($identification, $params); of type stdClass|SoapFault adds the type SoapFault to the return on line 73 which is incompatible with the return type declared by the interface SprykerEco\Zed\ArvatoRss...iCallInterface::execute of type stdClass.
Loading history...
64
65
        $this->apiCallLogger->log(
66
            $this->retrieveOrderNumber($params),
67
            static::CALL_TYPE,
68
            $result->Decision->ResultCode,
69
            $params,
70
            $result
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->sendRequest($identification, $params) on line 63 can also be of type object<SoapFault>; however, SprykerEco\Zed\ArvatoRss...lLoggerInterface::log() does only seem to accept object<stdClass>, 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...
71
        );
72
73
        return $result;
74
    }
75
76
    /**
77
     * @param \SoapClient $soapClient
78
     * @param array $params
79
     *
80
     * @return \stdClass|\SoapFault
81
     */
82
    abstract protected function executeCall(SoapClient $soapClient, array $params);
83
84
    /**
85
     * @param string $message
86
     *
87
     * @throws \Exception
88
     *
89
     * @return void
90
     */
91
    abstract protected function throwValidationException($message);
92
93
    /**
94
     * @param \Generated\Shared\Transfer\ArvatoRssIdentificationRequestTransfer $identification
95
     * @param array $params
96
     *
97
     * @return \SoapFault|\stdClass
98
     */
99
    protected function sendRequest(
100
        ArvatoRssIdentificationRequestTransfer $identification,
101
        array $params
102
    ) {
103
        $soapClient = $this->createSoapClient(
104
            $identification
105
        );
106
        $result = $this->executeCall($soapClient, $params);
107
        $this->validateResponse($result);
108
109
        return $result;
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\ArvatoRssIdentificationRequestTransfer $identification
114
     *
115
     * @return \SoapClient
116
     */
117
    protected function createSoapClient(ArvatoRssIdentificationRequestTransfer $identification)
118
    {
119
        $header = $this->requestHeaderConverter->convert($identification);
120
        $options = $this->getRequestOptions();
121
        $soapClient = new SoapClient(static::WSDL_PATH, $options);
122
        $soapClient->__setSoapHeaders($header);
123
        $soapClient->__setLocation(
124
            Config::get(ArvatoRssConstants::ARVATORSS_URL)
125
        );
126
127
        return $soapClient;
128
    }
129
130
    /**
131
     * @param \SoapFault|\stdClass $result
132
     *
133
     * @return void
134
     */
135
    protected function validateResponse($result)
136
    {
137
        if (is_soap_fault($result)) {
138
            $message = $this->extractExceptionMessage($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by parameter $result on line 135 can also be of type object<stdClass>; however, SprykerEco\Zed\ArvatoRss...tractExceptionMessage() does only seem to accept object<SoapFault>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
139
            $this->throwValidationException($message);
140
        }
141
    }
142
143
    /**
144
     * @param \SoapFault $result
145
     *
146
     * @return string
147
     */
148
    protected function extractExceptionMessage(SoapFault $result)
149
    {
150
        if (isset($result->detail) && !empty(array_keys(get_object_vars($result->detail))[0])) {
151
            $exceptionName = array_keys(get_object_vars($result->detail))[0];
0 ignored issues
show
Bug introduced by
The property detail does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
152
            $exceptionObj = $result->detail->{$exceptionName};
153
            return $exceptionObj->Description;
154
        }
155
156
        return $result->getMessage();
157
    }
158
159
    /**
160
     * @param array $params
161
     *
162
     * @return string
163
     */
164
    protected function retrieveOrderNumber(array $params)
165
    {
166
        return isset(
167
            $params[ArvatoRssRequestApiConfig::ARVATORSS_API_ORDER][ArvatoRssRequestApiConfig::ARVATORSS_API_ORDER_NUMBER]
168
        )?
169
            $params[ArvatoRssRequestApiConfig::ARVATORSS_API_ORDER][ArvatoRssRequestApiConfig::ARVATORSS_API_ORDER_NUMBER]:
170
            '';
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    protected function getRequestOptions()
177
    {
178
        return [
179
            'exceptions' => false,
180
        ];
181
    }
182
}
183