LookupInstallmentPlansCall   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 171
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 2
A buildLookupCustomerResponseTransfer() 0 17 3
A buildJsonRequestFromTransferObject() 0 5 1
A __construct() 0 12 1
A convertIntegerFieldsToDecimal() 0 4 1
A buildInstallmentPlanTransfer() 0 54 1
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\AfterPayToMoneyFacadeInterface;
20
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface;
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\AfterPayToUtilTextServiceInterface
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\AfterPayToMoneyFacadeInterface
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\AfterPayToUtilEncodingServiceInterface $utilEncoding
48
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface $money
49
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
50
     */
51
    public function __construct(
52
        ClientInterface $client,
53
        TransferToCamelCaseArrayConverterInterface $transferConverter,
54
        AfterPayToUtilEncodingServiceInterface $utilEncoding,
55
        AfterPayToMoneyFacadeInterface $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
        $amount = $this->money->convertIntegerToDecimal((int)$installmentPlansRequestTransfer->getAmount());
131
        $installmentPlansRequestTransfer->setAmount($amount);
132
    }
133
134
    /**
135
     * @param array $installmentPlanArray
136
     *
137
     * @return \Generated\Shared\Transfer\AfterPayInstallmentPlanTransfer
138
     */
139
    protected function buildInstallmentPlanTransfer(array $installmentPlanArray): AfterPayInstallmentPlanTransfer
140
    {
141
        return (new AfterPayInstallmentPlanTransfer())
142
            ->setBasketAmount(
143
                $this->money->convertDecimalToInteger(
144
                    $installmentPlanArray[AfterPayApiRequestConfig::BASKET_AMOUNT],
145
                ),
146
            )
147
            ->setInstallmentAmount(
148
                $this->money->convertDecimalToInteger(
149
                    $installmentPlanArray[AfterPayApiRequestConfig::INSTALLMENT_AMOUNT],
150
                ),
151
            )
152
            ->setFirstInstallmentAmount(
153
                $this->money->convertDecimalToInteger(
154
                    $installmentPlanArray[AfterPayApiRequestConfig::FIRST_INSTALLMENT_AMOUNT],
155
                ),
156
            )
157
            ->setLastInstallmentAmount(
158
                $this->money->convertDecimalToInteger(
159
                    $installmentPlanArray[AfterPayApiRequestConfig::LAST_INSTALLMENT_AMOUNT],
160
                ),
161
            )
162
            ->setTotalAmount(
163
                $this->money->convertDecimalToInteger(
164
                    $installmentPlanArray[AfterPayApiRequestConfig::TOTAL_AMOUNT],
165
                ),
166
            )
167
            ->setNumberOfInstallments(
168
                $installmentPlanArray[AfterPayApiRequestConfig::NUMBER_OF_INSTALLMENTS],
169
            )
170
            ->setInterestRate(
171
                $installmentPlanArray[AfterPayApiRequestConfig::INTEREST_RATE],
172
            )
173
            ->setEffectiveInterestRate(
174
                $installmentPlanArray[AfterPayApiRequestConfig::EFFECTIVE_INTEREST_RATE],
175
            )
176
            ->setEffectiveAnnualPercentageRate(
177
                $installmentPlanArray[AfterPayApiRequestConfig::EFFECTIVE_ANNUAL_PERCENTAGE_RATE],
178
            )
179
            ->setTotalInterestAmount(
180
                $installmentPlanArray[AfterPayApiRequestConfig::TOTAL_INTEREST_AMOUNT],
181
            )
182
            ->setStartupFee(
183
                $installmentPlanArray[AfterPayApiRequestConfig::STARTUP_FEE],
184
            )
185
            ->setMonthlyFee(
186
                $installmentPlanArray[AfterPayApiRequestConfig::MONTHLY_FEE],
187
            )
188
            ->setInstallmentProfileNumber(
189
                $installmentPlanArray[AfterPayApiRequestConfig::INSTALLMENT_PROFILE_NUMBER],
190
            )
191
            ->setReadMore(
192
                $installmentPlanArray[AfterPayApiRequestConfig::READ_MORE],
193
            );
194
    }
195
}
196