Passed
Push — feature/eco-574/eco-2266-check... ( efd21d )
by Aleksey
08:13
created

buildInstallmentPlanTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 1
dl 0
loc 59
rs 9.344
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Afterpay\Business\Api\Adapter\ApiCall;
9
10
use Generated\Shared\Transfer\AfterpayInstallmentPlansRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...entPlansRequestTransfer 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\AfterpayInstallmentPlansResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ntPlansResponseTransfer 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\AfterpayInstallmentPlanTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...InstallmentPlanTransfer 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 Spryker\Shared\Kernel\Transfer\AbstractTransfer;
14
use SprykerEco\Shared\Afterpay\AfterpayApiRequestConfig;
15
use SprykerEco\Zed\Afterpay\AfterpayConfig;
16
use SprykerEco\Zed\Afterpay\Business\Api\Adapter\Client\ClientInterface;
17
use SprykerEco\Zed\Afterpay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface;
18
use SprykerEco\Zed\Afterpay\Business\Exception\ApiHttpRequestException;
19
use SprykerEco\Zed\Afterpay\Dependency\Facade\AfterpayToMoneyInterface;
20
use SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilEncodingInterface;
21
22
class LookupInstallmentPlansCall extends AbstractApiCall implements LookupInstallmentPlansCallInterface
23
{
24
    /**
25
     * @var \SprykerEco\Zed\Afterpay\Business\Api\Adapter\Client\ClientInterface
26
     */
27
    protected $client;
28
29
    /**
30
     * @var \SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilTextInterface
31
     */
32
    protected $utilText;
33
34
    /**
35
     * @var \SprykerEco\Zed\Afterpay\AfterpayConfig
36
     */
37
    protected $config;
38
39
    /**
40
     * @var \SprykerEco\Zed\Afterpay\Dependency\Facade\AfterpayToMoneyInterface
41
     */
42
    protected $money;
43
44
    /**
45
     * @param \SprykerEco\Zed\Afterpay\Business\Api\Adapter\Client\ClientInterface $client
46
     * @param \SprykerEco\Zed\Afterpay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface $transferConverter
47
     * @param \SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilEncodingInterface $utilEncoding
48
     * @param \SprykerEco\Zed\Afterpay\Dependency\Facade\AfterpayToMoneyInterface $money
49
     * @param \SprykerEco\Zed\Afterpay\AfterpayConfig $config
50
     */
51
    public function __construct(
52
        ClientInterface $client,
53
        TransferToCamelCaseArrayConverterInterface $transferConverter,
54
        AfterpayToUtilEncodingInterface $utilEncoding,
55
        AfterpayToMoneyInterface $money,
56
        AfterpayConfig $config
57
    ) {
58
        $this->client = $client;
59
        $this->utilEncoding = $utilEncoding;
60
        $this->config = $config;
61
        $this->transferConverter = $transferConverter;
62
        $this->money = $money;
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\AfterpayInstallmentPlansRequestTransfer $installmentPlansRequestTransfer
67
     *
68
     * @return \Generated\Shared\Transfer\AfterpayInstallmentPlansResponseTransfer
69
     */
70
    public function execute(AfterpayInstallmentPlansRequestTransfer $installmentPlansRequestTransfer): AfterpayInstallmentPlansResponseTransfer
71
    {
72
        $jsonRequest = $this->buildJsonRequestFromTransferObject($installmentPlansRequestTransfer);
73
74
        try {
75
            $jsonResponse = $this->client->sendPost(
76
                $this->config->getLookupInstallmentPlansApiEndpointUrl(),
77
                $jsonRequest
78
            );
79
        } catch (ApiHttpRequestException $apiHttpRequestException) {
80
            $this->logApiException($apiHttpRequestException);
81
            $jsonResponse = '[]';
82
        }
83
84
        return $this->buildLookupCustomerResponseTransfer($jsonResponse);
85
    }
86
87
    /**
88
     * @param \Generated\Shared\Transfer\AfterpayInstallmentPlansRequestTransfer $installmentPlansRequestTransfer
89
     *
90
     * @return string
91
     */
92
    protected function buildJsonRequestFromTransferObject(AbstractTransfer $installmentPlansRequestTransfer): string
93
    {
94
        $this->convertIntegerFieldsToDecimal($installmentPlansRequestTransfer);
95
96
        return parent::buildJsonRequestFromTransferObject($installmentPlansRequestTransfer);
97
    }
98
99
    /**
100
     * @param string $jsonResponse
101
     *
102
     * @return \Generated\Shared\Transfer\AfterpayInstallmentPlansResponseTransfer
103
     */
104
    protected function buildLookupCustomerResponseTransfer(string $jsonResponse): AfterpayInstallmentPlansResponseTransfer
105
    {
106
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
107
108
        $responseTransfer = new AfterpayInstallmentPlansResponseTransfer();
109
110
        if (!isset($jsonResponseArray[AfterpayApiRequestConfig::AVAILABLE_PLANS])) {
111
            return $responseTransfer;
112
        }
113
114
        foreach ($jsonResponseArray[AfterpayApiRequestConfig::AVAILABLE_PLANS] as $planArray) {
115
            $responseTransfer->addInstallmentPlan(
116
                $this->buildInstallmentPlanTransfer($planArray)
117
            );
118
        }
119
120
        return $responseTransfer;
121
    }
122
123
    /**
124
     * @param \Generated\Shared\Transfer\AfterpayInstallmentPlansRequestTransfer $installmentPlansRequestTransfer
125
     *
126
     * @return void
127
     */
128
    protected function convertIntegerFieldsToDecimal(AfterpayInstallmentPlansRequestTransfer $installmentPlansRequestTransfer): void
129
    {
130
        $integerAmount = $installmentPlansRequestTransfer->getAmount();
131
        $installmentPlansRequestTransfer->setAmount(
132
            (string)$this->money->convertIntegerToDecimal($integerAmount)
133
        );
134
    }
135
136
    /**
137
     * @param array $installmentPlanArray
138
     *
139
     * @return \Generated\Shared\Transfer\AfterpayInstallmentPlanTransfer
140
     */
141
    protected function buildInstallmentPlanTransfer(array $installmentPlanArray): AfterpayInstallmentPlanTransfer
142
    {
143
        $installmentPlanTransfer = new AfterpayInstallmentPlanTransfer();
144
145
        $installmentPlanTransfer
146
            ->setBasketAmount(
147
                $this->money->convertDecimalToInteger(
148
                    $installmentPlanArray[AfterpayApiRequestConfig::BASKET_AMOUNT]
149
                )
150
            )
151
            ->setInstallmentAmount(
152
                $this->money->convertDecimalToInteger(
153
                    $installmentPlanArray[AfterpayApiRequestConfig::INSTALLMENT_AMOUNT]
154
                )
155
            )
156
            ->setFirstInstallmentAmount(
157
                $this->money->convertDecimalToInteger(
158
                    $installmentPlanArray[AfterpayApiRequestConfig::FIRST_INSTALLMENT_AMOUNT]
159
                )
160
            )
161
            ->setLastInstallmentAmount(
162
                $this->money->convertDecimalToInteger(
163
                    $installmentPlanArray[AfterpayApiRequestConfig::LAST_INSTALLMENT_AMOUNT]
164
                )
165
            )
166
            ->setTotalAmount(
167
                $this->money->convertDecimalToInteger(
168
                    $installmentPlanArray[AfterpayApiRequestConfig::TOTAL_AMOUNT]
169
                )
170
            )
171
            ->setNumberOfInstallments(
172
                $installmentPlanArray[AfterpayApiRequestConfig::NUMBER_OF_INSTALLMENTS]
173
            )
174
            ->setInterestRate(
175
                $installmentPlanArray[AfterpayApiRequestConfig::INTEREST_RATE]
176
            )
177
            ->setEffectiveInterestRate(
178
                $installmentPlanArray[AfterpayApiRequestConfig::EFFECTIVE_INTEREST_RATE]
179
            )
180
            ->setEffectiveAnnualPercentageRate(
181
                $installmentPlanArray[AfterpayApiRequestConfig::EFFECTIVE_ANNUAL_PERCENTAGE_RATE]
182
            )
183
            ->setTotalInterestAmount(
184
                $installmentPlanArray[AfterpayApiRequestConfig::TOTAL_INTEREST_AMOUNT]
185
            )
186
            ->setStartupFee(
187
                $installmentPlanArray[AfterpayApiRequestConfig::STARTUP_FEE]
188
            )
189
            ->setMonthlyFee(
190
                $installmentPlanArray[AfterpayApiRequestConfig::MONTHLY_FEE]
191
            )
192
            ->setInstallmentProfileNumber(
193
                $installmentPlanArray[AfterpayApiRequestConfig::INSTALLMENT_PROFILE_NUMBER]
194
            )
195
            ->setReadMore(
196
                $installmentPlanArray[AfterpayApiRequestConfig::READ_MORE]
197
            );
198
199
        return $installmentPlanTransfer;
200
    }
201
}
202