Passed
Push — feature/eco-3623-payone-pay-u-... ( 874a59...7427d2 )
by Roman
16:19 queued 08:34
created

AbstractMapper::getShippingAddressFromQuote()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 4
nc 3
nop 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\Yves\Computop\Mapper\Init;
9
10
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
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\ComputopAddressLineTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...utopAddressLineTransfer 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\ComputopAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ComputopAddressTransfer 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 Generated\Shared\Transfer\ComputopApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...putopApiRequestTransfer 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...
14
use Generated\Shared\Transfer\ComputopCountryTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ComputopCountryTransfer 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...
15
use Generated\Shared\Transfer\CountryCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...untryCollectionTransfer 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...
16
use Generated\Shared\Transfer\CountryTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CountryTransfer 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...
17
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
18
use Spryker\Shared\Kernel\Transfer\TransferInterface;
19
use Spryker\Yves\Router\Router\Router;
20
use Spryker\Yves\Router\Router\RouterInterface;
21
use SprykerEco\Service\ComputopApi\ComputopApiServiceInterface;
22
use SprykerEco\Shared\Computop\Config\ComputopApiConfig;
23
use SprykerEco\Yves\Computop\ComputopConfig;
24
use SprykerEco\Yves\Computop\ComputopConfigInterface;
25
use SprykerEco\Yves\Computop\Dependency\Client\ComputopToCountryClientInterface;
26
use SprykerEco\Yves\Computop\Dependency\ComputopToStoreInterface;
27
use SprykerEco\Yves\Computop\Dependency\Service\ComputopToUtilEncodingServiceInterface;
28
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
29
use Symfony\Component\HttpFoundation\Request;
30
31
abstract class AbstractMapper implements MapperInterface
32
{
33
    /**
34
     * @var \SprykerEco\Service\ComputopApi\ComputopApiServiceInterface
35
     */
36
    protected $computopApiService;
37
38
    /**
39
     * @var \Spryker\Yves\Router\Router\RouterInterface
40
     */
41
    protected $router;
42
43
    /**
44
     * @var \SprykerEco\Yves\Computop\Dependency\ComputopToStoreInterface
45
     */
46
    protected $store;
47
48
    /**
49
     * @var \SprykerEco\Yves\Computop\ComputopConfigInterface
50
     */
51
    protected $config;
52
53
    /**
54
     * @var \Symfony\Component\HttpFoundation\Request
55
     */
56
    protected $request;
57
58
    /**
59
     * @var \SprykerEco\Yves\Computop\Dependency\Service\ComputopToUtilEncodingServiceInterface
60
     */
61
    protected $utilEncodingService;
62
63
    /**
64
     * @var \SprykerEco\Yves\Computop\Dependency\Client\ComputopToCountryClientInterface
65
     */
66
    protected $countryClient;
67
68
    /**
69
     * @var array
70
     */
71
    protected $decryptedValues;
72
73
    /**
74
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
75
     *
76
     * @return \Spryker\Shared\Kernel\Transfer\TransferInterface
77
     */
78
    abstract protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer);
79
80
    /**
81
     * @param \SprykerEco\Service\ComputopApi\ComputopApiServiceInterface $computopApiService
82
     * @param \Spryker\Yves\Router\Router\RouterInterface $router
83
     * @param \SprykerEco\Yves\Computop\Dependency\ComputopToStoreInterface $store
84
     * @param \SprykerEco\Yves\Computop\ComputopConfigInterface $config
85
     * @param \Symfony\Component\HttpFoundation\Request $request
86
     * @param \SprykerEco\Yves\Computop\Dependency\Service\ComputopToUtilEncodingServiceInterface $utilEncodingService
87
     * @param \SprykerEco\Yves\Computop\Dependency\Client\ComputopToCountryClientInterface $countryClient
88
     */
89
    public function __construct(
90
        ComputopApiServiceInterface $computopApiService,
91
        RouterInterface $router,
92
        ComputopToStoreInterface $store,
93
        ComputopConfigInterface $config,
94
        Request $request,
95
        ComputopToUtilEncodingServiceInterface $utilEncodingService,
96
        ComputopToCountryClientInterface $countryClient
97
    ) {
98
        $this->computopApiService = $computopApiService;
99
        $this->router = $router;
100
        $this->store = $store;
101
        $this->config = $config;
102
        $this->request = $request;
103
        $this->utilEncodingService = $utilEncodingService;
104
        $this->countryClient = $countryClient;
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
109
     *
110
     * @return \Spryker\Shared\Kernel\Transfer\TransferInterface
111
     */
112
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer)
113
    {
114
        /** @var \Generated\Shared\Transfer\ComputopCreditCardPaymentTransfer $computopPaymentTransfer */
115
        $computopPaymentTransfer = $this->createTransferWithUnencryptedValues($quoteTransfer);
116
        $computopPaymentTransfer->setMerchantId($this->config->getMerchantId());
117
        $computopPaymentTransfer->setAmount($quoteTransfer->getTotals()->getGrandTotal());
118
        $computopPaymentTransfer->setCurrency($this->store->getCurrencyIsoCode());
119
        if ($quoteTransfer->getCurrency() && $quoteTransfer->getCurrency()->getCode() !== $computopPaymentTransfer->getCurrency()) {
120
            $computopPaymentTransfer->setCurrency($quoteTransfer->getCurrency()->getCode());
121
        }
122
        $computopPaymentTransfer->setResponse(ComputopConfig::RESPONSE_ENCRYPT_TYPE);
123
        $computopPaymentTransfer->setClientIp($this->getClientIp());
124
        $computopPaymentTransfer->setReqId($this->computopApiService->generateReqIdFromQuoteTransfer($quoteTransfer));
125
        $computopPaymentTransfer->setUrlFailure(
126
            $this->router->generate(ComputopRouteProviderPlugin::FAILURE_PATH_NAME, [], Router::ABSOLUTE_URL)
127
        );
128
        $computopPaymentTransfer->setUrlNotify(
129
            $this->router->generate(ComputopRouteProviderPlugin::NOTIFY_PATH_NAME, [], Router::ABSOLUTE_URL)
130
        );
131
        $computopPaymentTransfer->setShippingZip($this->getZipCode($quoteTransfer));
132
133
        return $computopPaymentTransfer;
134
    }
135
136
    /**
137
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
138
     *
139
     * @return string
140
     */
141
    protected function generateTransId(QuoteTransfer $quoteTransfer)
142
    {
143
        return $this->computopApiService->generateTransId($quoteTransfer);
144
    }
145
146
    /**
147
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
148
     * @param int $limit
149
     *
150
     * @return string
151
     */
152
    protected function getLimitedTransId(QuoteTransfer $quoteTransfer, $limit)
153
    {
154
        return substr($this->generateTransId($quoteTransfer), 0, $limit);
155
    }
156
157
    /**
158
     * @param string $path
159
     *
160
     * @return string
161
     */
162
    protected function getAbsoluteUrl($path)
163
    {
164
        return $this->config->getBaseUrlSsl() . $path;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    protected function getClientIp()
171
    {
172
        return $this->request->getClientIp();
173
    }
174
175
    /**
176
     * @param string $url
177
     * @param array $queryData
178
     *
179
     * @return string
180
     */
181
    protected function getActionUrl($url, $queryData)
182
    {
183
        return $url . '?' . http_build_query($queryData);
184
    }
185
186
    /**
187
     * @param string $merchantId
188
     * @param string $data
189
     * @param int $length
190
     *
191
     * @return array
192
     */
193
    protected function getQueryParameters($merchantId, $data, $length)
194
    {
195
        return [
196
            ComputopApiConfig::MERCHANT_ID => $merchantId,
197
            ComputopApiConfig::DATA => $data,
198
            ComputopApiConfig::LENGTH => $length,
199
            ComputopApiConfig::URL_BACK => $this->router->generate(
200
                $this->config->getCallbackFailureRedirectPath(),
201
                [],
202
                Router::ABSOLUTE_URL
203
            ),
204
        ];
205
    }
206
207
    /**
208
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $computopPaymentTransfer
209
     *
210
     * @return \Generated\Shared\Transfer\ComputopApiRequestTransfer
211
     */
212
    protected function createRequestTransfer(TransferInterface $computopPaymentTransfer)
213
    {
214
        return (new ComputopApiRequestTransfer())
215
            ->fromArray($computopPaymentTransfer->toArray(), true);
216
    }
217
218
    /**
219
     * @param string $method
220
     *
221
     * @return string
222
     */
223
    protected function getCaptureType(string $method): string
224
    {
225
        $paymentMethodsCaptureTypes = $this->config->getPaymentMethodsCaptureTypes();
226
227
        return $paymentMethodsCaptureTypes[$method] ?? '';
228
    }
229
230
    /**
231
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
232
     *
233
     * @return string
234
     */
235
    protected function getZipCode(QuoteTransfer $quoteTransfer): string
236
    {
237
        if ($quoteTransfer->getBillingSameAsShipping()) {
238
            return $quoteTransfer->getBillingAddress()->getZipCode();
239
        }
240
241
        $addressTransfer = $this->getShippingAddressFromQuote($quoteTransfer);
242
243
        return $addressTransfer->getZipCode();
244
    }
245
246
    /**
247
     * @param array $requestParameterData
248
     *
249
     * @return string
250
     */
251
    protected function encodeRequestParameterData(array $requestParameterData): string
252
    {
253
        $requestParameterData = $this->removeRedundantParams($requestParameterData);
254
255
        return base64_encode($this->utilEncodingService->encodeJson($requestParameterData));
0 ignored issues
show
Bug introduced by
It seems like $this->utilEncodingServi...($requestParameterData) can also be of type null; however, parameter $string of base64_encode() does only seem to accept string, 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

255
        return base64_encode(/** @scrutinizer ignore-type */ $this->utilEncodingService->encodeJson($requestParameterData));
Loading history...
256
    }
257
258
    /**
259
     * @param array $data
260
     *
261
     * @return array
262
     */
263
    protected function removeRedundantParams(array $data): array
264
    {
265
        $data = array_filter($data);
266
267
        foreach ($data as $key => $value) {
268
            if (is_array($value)) {
269
                $data[$key] = $this->removeRedundantParams($value);
270
            }
271
        }
272
273
        return $data;
274
    }
275
276
    /**
277
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
278
     *
279
     * @return \Generated\Shared\Transfer\AddressTransfer
280
     */
281
    protected function getShippingAddressFromQuote(QuoteTransfer $quoteTransfer): AddressTransfer
282
    {
283
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
284
            if ($itemTransfer->getShipment() && $itemTransfer->getShipment()->getShippingAddress()) {
285
                return $itemTransfer->getShipment()->getShippingAddress();
286
            }
287
        }
288
289
        return $quoteTransfer->getShippingAddressOrFail();
290
    }
291
292
    /**
293
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
294
     *
295
     * @return \Generated\Shared\Transfer\ComputopCountryTransfer
296
     */
297
    protected function getComputopCountryTransferFromQuote(AddressTransfer $addressTransfer): ComputopCountryTransfer
298
    {
299
        $countryTransfer = (new CountryTransfer())
300
            ->setIso2Code($addressTransfer->getIso2Code());
301
302
        $countryCollectionTransfer = (new CountryCollectionTransfer())
303
            ->addCountries($countryTransfer);
304
305
        $countryCollectionTransfer = $this->countryClient->findCountriesByIso2Codes($countryCollectionTransfer);
306
307
        return (new ComputopCountryTransfer())
308
            ->setCountryA3($countryCollectionTransfer->getCountries()->offsetGet(0)->getIso3Code());
309
    }
310
311
    /**
312
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
313
     *
314
     * @return \Generated\Shared\Transfer\ComputopAddressTransfer
315
     */
316
    protected function getComputopAddressTransferByAddressTransfer(AddressTransfer $addressTransfer): ComputopAddressTransfer
317
    {
318
        $computopAddressLineTransfer = (new ComputopAddressLineTransfer())
319
            ->setStreet($addressTransfer->getAddress1())
320
            ->setStreetNumber($addressTransfer->getAddress2());
321
322
        return (new ComputopAddressTransfer())
323
            ->setCity($addressTransfer->getCity())
324
            ->setCountry($this->getComputopCountryTransferFromQuote($addressTransfer))
325
            ->setAddressLine1($computopAddressLineTransfer)
326
            ->setPostalCode($addressTransfer->getZipCode());
327
    }
328
}
329