Passed
Branch [email protected] (3810d0)
by Bruno
11:09
created

AmountAndTwoInterestDataRequest::build()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 30
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 55
rs 8.5066

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
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Request;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use Getnet\PaymentMagento\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Getnet\PaymentMagento\Ga...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...
14
use Getnet\PaymentMagento\Gateway\SubjectReader;
15
use InvalidArgumentException;
16
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
17
use Magento\Payment\Gateway\Request\BuilderInterface;
18
19
/**
20
 * Class Amount And Two Interest Data Request - Payment amount structure.
21
 */
22
class AmountAndTwoInterestDataRequest implements BuilderInterface
23
{
24
    /**
25
     * Amount block name.
26
     */
27
    public const AMOUNT = 'amount';
28
29
    /**
30
     * @var SubjectReader
31
     */
32
    protected $subjectReader;
33
34
    /**
35
     * @var OrderAdapterFactory
36
     */
37
    protected $orderAdapterFactory;
38
39
    /**
40
     * @var Config
41
     */
42
    protected $config;
43
44
    /**
45
     * @var ConfigCc
46
     */
47
    protected $configCc;
48
49
    /**
50
     * @param SubjectReader       $subjectReader
51
     * @param OrderAdapterFactory $orderAdapterFactory
52
     * @param Config              $config
53
     * @param ConfigCc            $configCc
54
     */
55
    public function __construct(
56
        SubjectReader $subjectReader,
57
        OrderAdapterFactory $orderAdapterFactory,
58
        Config $config,
59
        ConfigCc $configCc
60
    ) {
61
        $this->subjectReader = $subjectReader;
62
        $this->orderAdapterFactory = $orderAdapterFactory;
63
        $this->config = $config;
64
        $this->configCc = $configCc;
65
    }
66
67
    /**
68
     * Build.
69
     *
70
     * @param array $buildSubject
71
     */
72
    public function build(array $buildSubject)
73
    {
74
        if (!isset($buildSubject['payment'])
75
        || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
76
        ) {
77
            throw new InvalidArgumentException('Payment data object should be provided');
78
        }
79
80
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
81
82
        $result = [];
83
84
        $order = $paymentDO->getOrder();
85
86
        $grandTotal = $order->getGrandTotalAmount();
87
88
        $payment = $paymentDO->getPayment();
89
90
        $installment = $payment->getAdditionalInformation('cc_installments') ?: 1;
91
92
        $totalFirst = $payment->getAdditionalInformation('cc_payment_first_amount');
93
94
        $amountFirst = $totalFirst;
95
96
        if ($installment > 1) {
97
            $storeId = $order->getStoreId();
98
            $interestFirst = $this->configCc->getInterestToAmount($installment, $totalFirst, $storeId);
99
            $amountFirst = $totalFirst + $interestFirst;
100
        }
101
102
        $totalSecondary = $grandTotal - $totalFirst;
103
104
        $installmentSecondary = $payment->getAdditionalInformation('cc_secondary_installments') ?: 1;
105
106
        $amountSecondary = $totalSecondary;
107
108
        if ($installmentSecondary > 1) {
109
            $storeId = $order->getStoreId();
110
            $interestSecondary = $this->configCc->getInterestToAmount($installmentSecondary, $totalSecondary, $storeId);
111
            $amountSecondary = $totalSecondary + $interestSecondary;
112
        }
113
114
        $result[self::AMOUNT] = $this->config->formatPrice(round($amountFirst + $amountSecondary, 2));
115
116
        $payment->setAdditionalInformation(
117
            'cc_payment_secondary_amount',
118
            round($amountSecondary, 2)
119
        );
120
121
        $payment->setAdditionalInformation(
122
            'cc_payment_first_amount',
123
            round($amountFirst, 2)
124
        );
125
126
        return $result;
127
    }
128
}
129