AbstractCall   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 162
rs 10
c 2
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A validateResponse() 0 5 2
A execute() 0 15 1
A createSoapClient() 0 11 1
A __construct() 0 6 1
A sendRequest() 0 11 1
A extractExceptionMessage() 0 10 3
A getRequestOptions() 0 4 1
A retrieveOrderNumber() 0 7 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\ArvatoRss\Business\Api\Adapter\ApiCall;
9
10
use Generated\Shared\Transfer\ArvatoRssIdentificationRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ficationRequestTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public const WSDL_PATH = __DIR__ . "/../../Etc/risk-solution-services.v2.1.wsdl";
25
26
    /**
27
     * @const string
28
     */
29
    public 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);
64
65
        $this->apiCallLogger->log(
66
            $this->retrieveOrderNumber($params),
67
            static::CALL_TYPE,
68
            $result->Decision->ResultCode,
0 ignored issues
show
Bug introduced by
The property Decision does not seem to exist on SoapFault.
Loading history...
69
            $params,
70
            $result
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type SoapFault; however, parameter $responsePayload of SprykerEco\Zed\ArvatoRss...lLoggerInterface::log() does only seem to accept stdClass, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            /** @scrutinizer ignore-type */ $result
Loading history...
71
        );
72
73
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type SoapFault which is incompatible with the documented return type stdClass.
Loading history...
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 can also be of type stdClass; however, parameter $result of SprykerEco\Zed\ArvatoRss...tractExceptionMessage() does only seem to accept SoapFault, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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