Passed
Push — master ( a2100c...af4f13 )
by Andrey
06:37
created

convertAddressToTransfer()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 128
nop 1
dl 0
loc 37
rs 4.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Amazonpay\Business\Api\Converter;
9
10
use Generated\Shared\Transfer\AddressTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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 Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ponseConstraintTransfer 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...
12
use Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...yResponseHeaderTransfer 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...
13
use PayWithAmazon\ResponseInterface;
14
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
15
16
abstract class AbstractResponseParserConverter extends AbstractConverter implements ResponseParserConverterInterface
17
{
18
19
    const STATUS_CODE_SUCCESS = 200;
20
21
    /**
22
     * @var string
23
     */
24
    protected $resultKeyName;
25
26
    /**
27
     * @return string
28
     */
29
    abstract protected function getResponseType();
30
31
    /**
32
     * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer
33
     */
34
    abstract protected function createTransferObject();
35
36
    /**
37
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer $responseTransfer
38
     * @param \PayWithAmazon\ResponseInterface $responseParser
39
     *
40
     * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer
41
     */
42
    protected function setBody(AbstractTransfer $responseTransfer, ResponseInterface $responseParser)
43
    {
44
        return $responseTransfer;
45
    }
46
47
    /**
48
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer $responseTransfer
49
     * @param \PayWithAmazon\ResponseInterface $responseParser
50
     *
51
     * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer
52
     */
53
    protected function setResponseDataToTransfer(AbstractTransfer $responseTransfer, ResponseInterface $responseParser)
54
    {
55
        $responseTransfer->setHeader($this->extractHeader($responseParser));
0 ignored issues
show
Bug introduced by
The method setHeader() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

55
        $responseTransfer->/** @scrutinizer ignore-call */ 
56
                           setHeader($this->extractHeader($responseParser));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        if ($responseTransfer->getHeader()->getIsSuccess()) {
0 ignored issues
show
Bug introduced by
The method getHeader() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

56
        if ($responseTransfer->/** @scrutinizer ignore-call */ getHeader()->getIsSuccess()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            return $this->setBody($responseTransfer, $responseParser);
58
        }
59
60
        return $responseTransfer;
61
    }
62
63
    /**
64
     * @param \PayWithAmazon\ResponseInterface $responseParser
65
     *
66
     * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer
67
     */
68
    public function convert(ResponseInterface $responseParser)
69
    {
70
        return $this->setResponseDataToTransfer($this->createTransferObject(), $responseParser);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setRespons...ect(), $responseParser) returns the type Spryker\Shared\Kernel\Transfer\AbstractTransfer which is incompatible with the return type mandated by SprykerEco\Zed\Amazonpay...terInterface::convert() of Generated\Shared\Transfer\AbstractTransfer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
71
    }
72
73
    /**
74
     * @param \PayWithAmazon\ResponseInterface $responseParser
75
     *
76
     * @return array
77
     */
78
    protected function extractMetadata(ResponseInterface $responseParser)
79
    {
80
        return empty($responseParser->toArray()['ResponseMetadata'])
81
            ? []
82
            : $responseParser->toArray()['ResponseMetadata'];
83
    }
84
85
    /**
86
     * @param \PayWithAmazon\ResponseInterface $responseParser
87
     *
88
     * @return int
89
     */
90
    protected function extractStatusCode(ResponseInterface $responseParser)
91
    {
92
        return (int)$responseParser->toArray()['ResponseStatus'];
93
    }
94
95
    /**
96
     * @param \PayWithAmazon\ResponseInterface $responseParser
97
     *
98
     * @return \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer
99
     */
100
    protected function extractHeader(ResponseInterface $responseParser)
101
    {
102
        $statusCode = $this->extractStatusCode($responseParser);
103
        $metadata = $this->extractMetadata($responseParser);
104
        $constraints = $this->extractConstraints($responseParser);
105
106
        $header = new AmazonpayResponseHeaderTransfer();
107
        $header->setIsSuccess($this->isSuccess($responseParser));
108
        $header->setStatusCode($statusCode);
109
110
        if ($metadata) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $metadata of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
111
            $header->setRequestId($metadata['RequestId']);
112
        }
113
114
        if (!empty($responseParser->toArray()['Error'])) {
115
            $header->setErrorMessage($responseParser->toArray()['Error']['Message']);
116
            $header->setErrorCode($responseParser->toArray()['Error']['Code']);
117
            $header->setRequestId($responseParser->toArray()['RequestId']);
118
119
            return $header;
120
        }
121
122
        if ($constraints) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $constraints of type Generated\Shared\Transfe...nseConstraintTransfer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
123
            $header->setConstraints($constraints);
124
        }
125
126
        return $header;
127
    }
128
129
    /**
130
     * @param \PayWithAmazon\ResponseInterface $responseParser
131
     *
132
     * @return bool
133
     */
134
    protected function isSuccess(ResponseInterface $responseParser)
135
    {
136
        return
137
            $this->extractStatusCode($responseParser) == self::STATUS_CODE_SUCCESS
138
            && empty($this->extractConstraints($responseParser));
139
    }
140
141
    /**
142
     * @param \PayWithAmazon\ResponseInterface $responseParser
143
     *
144
     * @return array
145
     */
146
    protected function extractResult(ResponseInterface $responseParser)
147
    {
148
        $responseType = $this->getResponseType();
149
150
        return !empty($responseParser->toArray()[$responseType])
151
            ? $responseParser->toArray()[$responseType]
152
            : [];
153
    }
154
155
    /**
156
     * @param \PayWithAmazon\ResponseInterface $responseParser
157
     *
158
     * @return \Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer[]
159
     */
160
    protected function extractConstraints(ResponseInterface $responseParser)
161
    {
162
        $result = $this->extractResult($responseParser);
163
164
        if (empty($result['OrderReferenceDetails']['Constraints'])) {
165
            return [];
166
        }
167
168
        $constraintTransfers = [];
169
170
        if (count($result['OrderReferenceDetails']['Constraints']) === 1) {
171
            $constraints = array_values($result['OrderReferenceDetails']['Constraints']);
172
        } else {
173
            $constraints = $result['OrderReferenceDetails']['Constraints'];
174
        }
175
176
        foreach ($constraints as $constraint) {
177
            $constraintTransfer = new AmazonpayResponseConstraintTransfer();
178
            $constraintTransfer->setConstraintId($constraint['ConstraintID']);
179
            $constraintTransfer->setConstraintId($constraint['Description']);
180
181
            $constraintTransfers[] = $constraintTransfer;
182
        }
183
184
        return $constraintTransfers;
185
    }
186
187
    /**
188
     * @param \PayWithAmazon\ResponseInterface $responseParser
189
     *
190
     * @return \Generated\Shared\Transfer\AddressTransfer
191
     */
192
    protected function extractShippingAddress(ResponseInterface $responseParser)
193
    {
194
        $address = new AddressTransfer();
195
196
        if (!$this->isSuccess($responseParser)) {
197
            return $address;
198
        }
199
200
        $aResponseAddress =
201
            $this->extractResult($responseParser)['OrderReferenceDetails']['Destination']['PhysicalDestination'];
202
203
        return $this->convertAddressToTransfer($aResponseAddress);
204
    }
205
206
    /**
207
     * @param array $addressData
208
     *
209
     * @return \Generated\Shared\Transfer\AddressTransfer
210
     */
211
    protected function convertAddressToTransfer(array $addressData)
212
    {
213
        $address = new AddressTransfer();
214
215
        $address->setCity($addressData['City']);
216
        $address->setIso2Code($addressData['CountryCode']);
217
        $address->setZipCode($addressData['PostalCode']);
218
219
        if (!empty($addressData['Name'])) {
220
            $address = $this->updateNameData($address, $addressData['Name']);
221
        }
222
223
        if (!empty($addressData['AddressLine1'])) {
224
            $address->setAddress1($addressData['AddressLine1']);
0 ignored issues
show
Bug introduced by
The method setAddress1() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

224
            $address->/** @scrutinizer ignore-call */ 
225
                      setAddress1($addressData['AddressLine1']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
225
        }
226
227
        if (!empty($addressData['AddressLine2'])) {
228
            $address->setAddress2($addressData['AddressLine2']);
0 ignored issues
show
Bug introduced by
The method setAddress2() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

228
            $address->/** @scrutinizer ignore-call */ 
229
                      setAddress2($addressData['AddressLine2']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
        }
230
231
        if (!empty($addressData['AddressLine3'])) {
232
            $address->setAddress3($addressData['AddressLine3']);
0 ignored issues
show
Bug introduced by
The method setAddress3() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

232
            $address->/** @scrutinizer ignore-call */ 
233
                      setAddress3($addressData['AddressLine3']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
233
        }
234
235
        if (!empty($addressData['District'])) {
236
            $address->setRegion($addressData['District']);
0 ignored issues
show
Bug introduced by
The method setRegion() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

236
            $address->/** @scrutinizer ignore-call */ 
237
                      setRegion($addressData['District']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
237
        }
238
239
        if (!empty($addressData['StateOrRegion'])) {
240
            $address->setState($addressData['StateOrRegion']);
0 ignored issues
show
Bug introduced by
The method setState() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

240
            $address->/** @scrutinizer ignore-call */ 
241
                      setState($addressData['StateOrRegion']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
        }
242
243
        if (!empty($addressData['Phone'])) {
244
            $address->setPhone($addressData['Phone']);
0 ignored issues
show
Bug introduced by
The method setPhone() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

244
            $address->/** @scrutinizer ignore-call */ 
245
                      setPhone($addressData['Phone']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
245
        }
246
247
        return $address;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $address also could return the type Spryker\Shared\Kernel\Transfer\AbstractTransfer which is incompatible with the documented return type Generated\Shared\Transfer\AddressTransfer.
Loading history...
248
    }
249
250
}
251