Completed
Pull Request — master (#3)
by Aleksey
15:31 queued 12:58
created

convertRequestTransferToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.8333
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\CrefoPayApi\Business\Request\Converter;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\CrefoPayApiAmountTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiAmountTransfer 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\CrefoPayApiBasketItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ayApiBasketItemTransfer 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\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayApiReserveInformationRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ormationRequestTransfer 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 SprykerEco\Zed\CrefoPayApi\Business\Request\CrefoPayApiRequestFields;
16
17
class ReserveRequestConverter implements CrefoPayApiRequestConverterInterface
18
{
19
    /**
20
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
21
     *
22
     * @return array
23
     */
24
    public function convertRequestTransferToArray(CrefoPayApiRequestTransfer $requestTransfer): array
25
    {
26
        $reserveRequest = $requestTransfer->getReserveRequest();
27
        if ($reserveRequest === null) {
28
            return [];
29
        }
30
31
        return [
32
            CrefoPayApiRequestFields::API_FIELD_MERCHANT_ID => $reserveRequest->getMerchantID(),
33
            CrefoPayApiRequestFields::API_FIELD_STORE_ID => $reserveRequest->getStoreID(),
34
            CrefoPayApiRequestFields::API_FIELD_ORDER_ID => $reserveRequest->getOrderID(),
35
            CrefoPayApiRequestFields::API_FIELD_PAYMENT_METHOD => $reserveRequest->getPaymentMethod(),
36
            CrefoPayApiRequestFields::API_FIELD_PAYMENT_INSTRUMENT_ID => $reserveRequest->getPaymentInstrumentID(),
37
            CrefoPayApiRequestFields::API_FIELD_ADDITIONAL_INFORMATION => $this->getAdditionalInformationData($reserveRequest->getAdditionalInformation()),
38
            CrefoPayApiRequestFields::API_FIELD_AMOUNT => $this->getAmountData($reserveRequest->getAmount()),
39
            CrefoPayApiRequestFields::API_FIELD_BASKET_ITEMS => $this->getBasketItemsData($reserveRequest->getBasketItems()),
40
            CrefoPayApiRequestFields::API_FIELD_CVV => $reserveRequest->getCvv(),
41
        ];
42
    }
43
44
    /**
45
     * @param \Generated\Shared\Transfer\CrefoPayApiReserveInformationRequestTransfer|null $reserveInformationRequestTransfer
46
     *
47
     * @return array|null
48
     */
49
    protected function getAdditionalInformationData(?CrefoPayApiReserveInformationRequestTransfer $reserveInformationRequestTransfer): ?array
50
    {
51
        return $reserveInformationRequestTransfer ? $this->convertAdditionalInformationTransferToArray($reserveInformationRequestTransfer) : null;
52
    }
53
54
    /**
55
     * @param \Generated\Shared\Transfer\CrefoPayApiReserveInformationRequestTransfer $reserveInformationRequestTransfer
56
     *
57
     * @return array
58
     */
59
    protected function convertAdditionalInformationTransferToArray(CrefoPayApiReserveInformationRequestTransfer $reserveInformationRequestTransfer): array
60
    {
61
        return [
62
            CrefoPayApiRequestFields::API_OBJECT_ADDITIONAL_INFORMATION_FIELD_SALUTATION => $reserveInformationRequestTransfer->getSalutation(),
63
            CrefoPayApiRequestFields::API_OBJECT_ADDITIONAL_INFORMATION_FIELD_DATE_OF_BIRTH => $reserveInformationRequestTransfer->getDateOfBirth(),
64
        ];
65
    }
66
67
    /**
68
     * @param \ArrayObject|\Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer[] $basketItems
69
     *
70
     * @return array|null
71
     */
72
    protected function getBasketItemsData(ArrayObject $basketItems): ?array
73
    {
74
        if ($basketItems->count() === 0) {
75
            return null;
76
        }
77
78
        return array_map(
79
            function (CrefoPayApiBasketItemTransfer $basketItem) {
80
                return $this->convertBasketItemTransferToArray($basketItem);
81
            },
82
            $basketItems->getArrayCopy()
83
        );
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer $basketItem
88
     *
89
     * @return array
90
     */
91
    protected function convertBasketItemTransferToArray(CrefoPayApiBasketItemTransfer $basketItem): array
92
    {
93
        return [
94
            CrefoPayApiRequestFields::API_OBJECT_BASKET_ITEM_FIELD_TEXT => $basketItem->getBasketItemText(),
95
            CrefoPayApiRequestFields::API_OBJECT_BASKET_ITEM_FIELD_ID => $basketItem->getBasketItemID(),
96
            CrefoPayApiRequestFields::API_OBJECT_BASKET_ITEM_FIELD_COUNT => $basketItem->getBasketItemCount(),
97
            CrefoPayApiRequestFields::API_OBJECT_BASKET_ITEM_FIELD_AMOUNT => $this->getAmountData($basketItem->getBasketItemAmount()),
98
            CrefoPayApiRequestFields::API_OBJECT_BASKET_ITEM_FIELD_RISK_CLASS => $basketItem->getBasketItemRiskClass(),
99
            CrefoPayApiRequestFields::API_OBJECT_BASKET_ITEM_FIELD_TYPE => $basketItem->getBasketItemType(),
100
        ];
101
    }
102
103
    /**
104
     * @param \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null $amountTransfer
105
     *
106
     * @return array|null
107
     */
108
    protected function getAmountData(?CrefoPayApiAmountTransfer $amountTransfer): ?array
109
    {
110
        return $amountTransfer ? $this->convertAmountTransferToArray($amountTransfer) : null;
111
    }
112
113
    /**
114
     * @param \Generated\Shared\Transfer\CrefoPayApiAmountTransfer $amountTransfer
115
     *
116
     * @return array
117
     */
118
    protected function convertAmountTransferToArray(CrefoPayApiAmountTransfer $amountTransfer): array
119
    {
120
        return [
121
            CrefoPayApiRequestFields::API_OBJECT_AMOUNT_FIELD_AMOUNT => $amountTransfer->getAmount(),
122
            CrefoPayApiRequestFields::API_OBJECT_AMOUNT_FIELD_VAT_AMOUNT => $amountTransfer->getVatAmount(),
123
            CrefoPayApiRequestFields::API_OBJECT_AMOUNT_FIELD_VAT_RATE => $amountTransfer->getVatRate(),
124
        ];
125
    }
126
}
127