Cc::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 16
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Block\Sales\Form;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config as ConfigBase;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use Getnet\PaymentMagento\Gateway\Config\ConfigCcVault;
14
use Magento\Backend\Model\Session\Quote;
15
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
16
use Magento\Framework\Url;
17
use Magento\Framework\View\Element\Template\Context;
18
use Magento\Payment\Block\Form\Cc as NativeCc;
19
use Magento\Payment\Helper\Data;
20
use Magento\Payment\Model\Config;
21
use Magento\Vault\Model\VaultPaymentInterface;
22
23
/**
24
 * Class Cc - Form for payment by cc.
25
 */
26
class Cc extends NativeCc
27
{
28
    /**
29
     * Cc template.
30
     *
31
     * @var string
32
     */
33
    protected $_template = 'Getnet_PaymentMagento::form/cc.phtml';
34
35
    /**
36
     * @var Quote
37
     */
38
    protected $session;
39
40
    /**
41
     * @var ConfigCc
42
     */
43
    protected $configCc;
44
45
    /**
46
     * @var Data
47
     */
48
    private $paymentDataHelper;
49
50
    /**
51
     * @var ConfigBase
52
     */
53
    private $configBase;
54
55
    /**
56
     * @var PriceHelper
57
     */
58
    private $priceHelper;
59
60
    /**
61
     * @param Context     $context
62
     * @param Config      $paymentConfig
63
     * @param Quote       $session
64
     * @param ConfigCc    $configCc
65
     * @param configBase  $configBase
66
     * @param Data        $paymentDataHelper
67
     * @param PriceHelper $priceHelper
68
     * @param array       $data
69
     */
70
    public function __construct(
71
        Context $context,
72
        Config $paymentConfig,
73
        Quote $session,
74
        ConfigCc $configCc,
75
        ConfigBase $configBase,
76
        Data $paymentDataHelper,
77
        PriceHelper $priceHelper,
78
        array $data = []
79
    ) {
80
        parent::__construct($context, $paymentConfig, $data);
81
        $this->session = $session;
82
        $this->configBase = $configBase;
83
        $this->configCc = $configCc;
84
        $this->priceHelper = $priceHelper;
85
        $this->paymentDataHelper = $paymentDataHelper;
86
    }
87
88
    /**
89
     * Title - Cc.
90
     *
91
     * @return string
92
     */
93
    public function getTitle(): ?string
94
    {
95
        return $this->configCc->getTitle();
96
    }
97
98
    /**
99
     * Url For Tokenize.
100
     *
101
     * @var string
102
     */
103
    public function getUrlForTokenize()
104
    {
105
        return $this->getUrl('payment/order/NumberToken');
106
    }
107
108
    /**
109
     * Use tax document capture - Cc.
110
     *
111
     * @var bool
112
     */
113
    public function getTaxDocumentCapture()
114
    {
115
        return $this->configCc->hasUseTaxDocumentCapture();
116
    }
117
118
    /**
119
     * Use phone capture - Cc.
120
     *
121
     * @var bool
122
     */
123
    public function getPhoneCapture()
124
    {
125
        return $this->configCc->hasUsePhoneCapture();
126
    }
127
128
    /**
129
     * Get configured vault payment.
130
     *
131
     * @return VaultPaymentInterface
132
     */
133
    private function getVaultPayment()
134
    {
135
        return $this->paymentDataHelper->getMethodInstance(ConfigCcVault::METHOD);
136
    }
137
138
    /**
139
     * Check if vault enabled.
140
     *
141
     * @return bool
142
     */
143
    public function isVaultEnabled()
144
    {
145
        $vaultPayment = $this->getVaultPayment();
146
147
        return $vaultPayment->isActive();
148
    }
149
150
    /**
151
     * Select Installment - Cc.
152
     *
153
     * @return array
154
     */
155
    public function getSelectInstallments(): array
156
    {
157
        $total = $this->session->getQuote()->getGrandTotal();
158
        $installments = $this->getInstallments($total);
159
160
        return $installments;
161
    }
162
163
    /**
164
     * Installments - Cc.
165
     *
166
     * @param float $amount
167
     *
168
     * @return array
169
     */
170
    public function getInstallments($amount): array
171
    {
172
        $interestByInst = $this->configCc->getInfoInterest();
173
        $plotlist = [];
174
        foreach ($interestByInst as $key => $interest) {
175
            if ($key > 0) {
176
                $plotValue = $this->getInterestSimple($amount, $interest, $key);
177
                $plotValue = number_format((float) $plotValue, 2, '.', '');
178
                $installmentPrice = $this->priceHelper->currency($plotValue, true, false);
0 ignored issues
show
Bug introduced by
$plotValue of type string is incompatible with the type double expected by parameter $value of Magento\Framework\Pricing\Helper\Data::currency(). ( Ignorable by Annotation )

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

178
                $installmentPrice = $this->priceHelper->currency(/** @scrutinizer ignore-type */ $plotValue, true, false);
Loading history...
179
                $plotlist[$key] = $key.__('x of ').$installmentPrice;
180
            }
181
        }
182
183
        return $plotlist;
184
    }
185
186
    /**
187
     * Interest Simple - Cc.
188
     *
189
     * @param float $total
190
     * @param float $interest
191
     * @param int   $portion
192
     *
193
     * @return float
194
     */
195
    public function getInterestSimple($total, $interest, $portion): float
196
    {
197
        if ($interest) {
198
            $taxa = $interest / 100;
199
            $valinterest = $total * $taxa;
200
201
            return ($total + $valinterest) / $portion;
202
        }
203
204
        return $total / $portion;
205
    }
206
}
207