Passed
Push[email protected] ( 1b717f...679cdf )
by Bruno
11:41 queued 08:46
created

SellerDataRequest::build()   F

Complexity

Conditions 18
Paths 209

Size

Total Lines 100
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 64
c 1
b 0
f 0
nc 209
nop 1
dl 0
loc 100
rs 3.9208

How to fix   Long Method    Complexity   

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
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Request;
10
11
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Pricing\Helper\Data 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 Magento\Payment\Gateway\Request\BuilderInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Request\BuilderInterface 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 Moip\Magento2\Gateway\Config\Config;
14
use Moip\Magento2\Gateway\Config\ConfigCc;
15
use Moip\Magento2\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Da...der\OrderAdapterFactory 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 Moip\Magento2\Gateway\SubjectReader;
17
18
/**
19
 * Class SellerDataRequest - Order seller amount structure.
20
 */
21
class SellerDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Receivers block name.
25
     */
26
    const RECEIVERS = 'receivers';
27
28
    /**
29
     * Moip Account block name.
30
     */
31
    const RECEIVERS_MOIP_ACCOUNT = 'moipAccount';
32
33
    /**
34
     * Moip Account Id block name.
35
     */
36
    const RECEIVERS_MOIP_ACCOUNT_ID = 'id';
37
38
    /**
39
     * Type Receiver block name.
40
     */
41
    const RECEIVERS_TYPE = 'type';
42
43
    /**
44
     * Secondary Type Receiver.
45
     * required.
46
     */
47
    const RECEIVERS_TYPE_SECONDARY = 'SECONDARY';
48
49
    /**
50
     * Amount Receiver block name.
51
     */
52
    const RECEIVERS_AMOUNT = 'amount';
53
54
    /**
55
     * Fixed Receiver Type block name.
56
     */
57
    const RECEIVERS_TYPE_FIXED = 'fixed';
58
59
    /**
60
     * Percent Receiver Type block name.
61
     */
62
    const RECEIVERS_TYPE_PERCENT = 'percent';
63
64
    /**
65
     * @var SubjectReader
66
     */
67
    private $subjectReader;
68
69
    /**
70
     * @var OrderAdapterFactory
71
     */
72
    private $orderAdapterFactory;
73
74
    /**
75
     * @var Config
76
     */
77
    private $config;
78
79
    /**
80
     * @var configCc
81
     */
82
    private $configCc;
83
84
    /**
85
     * @var priceHelper
86
     */
87
    private $priceHelper;
88
89
    /**
90
     * @param SubjectReader       $subjectReader
91
     * @param OrderAdapterFactory $orderAdapterFactory
92
     * @param Config              $Config
93
     * @param ConfigCc            $ConfigCc
94
     * @param CheckoutHelper      $checkoutHelper
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Request\CheckoutHelper 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...
95
     */
96
    public function __construct(
97
        SubjectReader $subjectReader,
98
        OrderAdapterFactory $orderAdapterFactory,
99
        Config $config,
100
        ConfigCc $configCc,
101
        PriceHelper $checkoutHelper
102
    ) {
103
        $this->subjectReader = $subjectReader;
104
        $this->orderAdapterFactory = $orderAdapterFactory;
105
        $this->config = $config;
106
        $this->configCc = $configCc;
107
        $this->priceHelper = $checkoutHelper;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function build(array $buildSubject)
114
    {
115
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
116
        $payment = $paymentDO->getPayment();
117
118
        $result = [];
119
120
        $orderAdapter = $this->orderAdapterFactory->create(
121
            ['order' => $payment->getOrder()]
122
        );
123
124
        $order = $paymentDO->getOrder();
125
126
        $storeId = $order->getStoreId();
127
        $useSplit = $this->config->getSplitValue('use_split', $storeId);
128
        if (!$useSplit) {
129
            return $result;
130
        }
131
132
        $addition = $orderAdapter->getTaxAmount();
133
        $interest = $orderAdapter->getBaseMoipInterestAmount();
134
        $grandTotal = $order->getGrandTotalAmount();
135
        $discount = $orderAdapter->getDiscountAmount();
0 ignored issues
show
Unused Code introduced by
The assignment to $discount is dead and can be removed.
Loading history...
136
        $total = $grandTotal + $interest;
137
        if ($interest > 0) {
138
            $total = $grandTotal - $interest;
139
        }
140
141
        $secondaryMPA = $this->config->getSplitValue('secondary_mpa', $storeId);
142
        $secondaryPercent = $this->config->getSplitValue('secondary_percent', $storeId);
143
        $commiUseShipping = $this->config->getSplitValue('secondary_percent_include_shipping', $storeId);
144
        $commiUseInterest = $this->config->getSplitValue('secondary_percent_include_interest', $storeId);
145
146
        if ($payment->getMethod() === 'moip_magento2_cc' || $payment->getMethod() === 'moip_magento2_cc_vault') {
147
            if ($installment = $payment->getAdditionalInformation('cc_installments')) {
148
                $interestInfo = $this->configCc->getInfoInterest($storeId);
149
                if ($installment > 1) {
150
                    $typeInstallment = $this->configCc->getTypeInstallment($storeId);
151
                    if ($interestInfo[$installment] > 0) {
152
                        $installmentInterest = $this->getInterestCompound($total, $interestInfo[$installment], $installment);
153
                        if ($typeInstallment === 'simple') {
154
                            $installmentInterest = $this->getInterestSimple($total, $interestInfo[$installment], $installment);
155
                        }
156
157
                        if ($installmentInterest) {
158
                            $installmentInterest = number_format((float) $installmentInterest, 2, '.', '');
159
                            $payment->setAdditionalInformation(
160
                                self::INSTALLMENT_INTEREST,
161
                                $this->priceHelper->currency($installmentInterest, true, false)
162
                            );
163
                            if (!$interest) {
164
                                $orderAdapter->setMoipInterestAmount($installmentInterest)->setBaseMoipInterestAmount($installmentInterest);
165
                            }
166
                            $addition = $addition + $installmentInterest;
167
                        }
168
                    }
169
                } elseif ((int) $installment === 1) {
170
                    if ($interestInfo[$installment] < 0) {
171
                        $totalWithDiscount = $grandTotal + ($interest * -1);
172
                        $discountInterest = $this->getInterestDiscount($totalWithDiscount, $interestInfo[$installment]);
173
                        $discountInterest = number_format((float) $discountInterest, 2, '.', '');
174
175
                        $payment->setAdditionalInformation(
176
                            self::INSTALLMENT_INTEREST,
177
                            $this->priceHelper->currency($discountInterest, true, false)
178
                        );
179
                        if (!$interest) {
180
                            $orderAdapter->setMoipInterestAmount($discountInterest)->setBaseMoipInterestAmount($discountInterest);
181
                        }
182
                        $interest = $discountInterest;
183
                    }
184
                }
185
            }
186
        }
187
188
        if (!$commiUseShipping) {
189
            $total = $total - $orderAdapter->getShippingAmount();
190
        }
191
192
        if ($commiUseInterest) {
193
            if ($interest > 0) {
194
                $total = $total + $addition;
195
            } elseif ($interest < 0) {
196
                $total = $total + $interest;
197
            }
198
        }
199
200
        $commission = $total * ($secondaryPercent / 100);
201
202
        $result[self::RECEIVERS][] = [
203
            self::RECEIVERS_MOIP_ACCOUNT => [
204
                self::RECEIVERS_MOIP_ACCOUNT_ID => $secondaryMPA,
205
            ],
206
            self::RECEIVERS_TYPE   => self::RECEIVERS_TYPE_SECONDARY,
207
            self::RECEIVERS_AMOUNT => [
208
                self::RECEIVERS_TYPE_FIXED => $this->config->formatPrice(round($commission, 2)),
0 ignored issues
show
Bug introduced by
round($commission, 2) of type double is incompatible with the type integer expected by parameter $amount of Moip\Magento2\Gateway\Config\Config::formatPrice(). ( Ignorable by Annotation )

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

208
                self::RECEIVERS_TYPE_FIXED => $this->config->formatPrice(/** @scrutinizer ignore-type */ round($commission, 2)),
Loading history...
209
            ],
210
        ];
211
212
        return $result;
213
    }
214
215
    /**
216
     * Get Intereset Discount.
217
     *
218
     * @param $total
219
     * @param $interest
220
     *
221
     * @return string
222
     */
223
    public function getInterestDiscount($total, $interest)
224
    {
225
        if ($interest) {
226
            $taxa = $interest / 100;
227
            $valinterest = $total * $taxa;
228
229
            return $valinterest;
230
        }
231
232
        return 0;
233
    }
234
235
    /**
236
     * Get Intereset for Simple.
237
     *
238
     * @param $total
239
     * @param $interest
240
     * @param $portion
241
     *
242
     * @return string
243
     */
244
    public function getInterestSimple($total, $interest, $portion)
245
    {
246
        if ($interest) {
247
            $taxa = $interest / 100;
248
            $valinterest = $total * $taxa;
249
250
            return ($total + $valinterest) / $portion;
251
        }
252
253
        return 0;
254
    }
255
256
    /**
257
     * Get Intereset for Compound.
258
     *
259
     * @param $total
260
     * @param $interest
261
     * @param $portion
262
     *
263
     * @return string
264
     */
265
    public function getInterestCompound($total, $interest, $portion)
266
    {
267
        if ($interest) {
268
            $taxa = $interest / 100;
269
270
            return (($total * $taxa) * 1) / (1 - (pow(1 / (1 + $taxa), $portion)));
271
        }
272
273
        return 0;
274
    }
275
}
276