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

TwoCc::getSelectInstallments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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 Magento\Backend\Model\Session\Quote;
14
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
15
use Magento\Framework\Url;
16
use Magento\Framework\View\Element\Template\Context;
17
use Magento\Payment\Block\Form\Cc as NativeCc;
18
use Magento\Payment\Helper\Data;
19
use Magento\Payment\Model\Config;
20
21
/**
22
 * Class TwoCc - Form for payment by cc.
23
 */
24
class TwoCc extends NativeCc
25
{
26
    /**
27
     * Cc template.
28
     *
29
     * @var string
30
     */
31
    protected $_template = 'Getnet_PaymentMagento::form/cc.phtml';
32
33
    /**
34
     * @var Quote
35
     */
36
    protected $session;
37
38
    /**
39
     * @var ConfigCc
40
     */
41
    protected $configCc;
42
43
    /**
44
     * @var Data
45
     */
46
    private $paymentDataHelper;
47
48
    /**
49
     * @var ConfigBase
50
     */
51
    private $configBase;
52
53
    /**
54
     * @var PriceHelper
55
     */
56
    private $priceHelper;
57
58
    /**
59
     * @param Context     $context
60
     * @param Config      $paymentConfig
61
     * @param Quote       $session
62
     * @param ConfigCc    $configCc
63
     * @param configBase  $configBase
64
     * @param Data        $paymentDataHelper
65
     * @param PriceHelper $priceHelper
66
     * @param array       $data
67
     */
68
    public function __construct(
69
        Context $context,
70
        Config $paymentConfig,
71
        Quote $session,
72
        ConfigCc $configCc,
73
        ConfigBase $configBase,
74
        Data $paymentDataHelper,
75
        PriceHelper $priceHelper,
76
        array $data = []
77
    ) {
78
        parent::__construct($context, $paymentConfig, $data);
79
        $this->session = $session;
80
        $this->configBase = $configBase;
81
        $this->configCc = $configCc;
82
        $this->priceHelper = $priceHelper;
83
        $this->paymentDataHelper = $paymentDataHelper;
84
    }
85
86
    /**
87
     * Title - Cc.
88
     *
89
     * @return string
90
     */
91
    public function getTitle(): ?string
92
    {
93
        return $this->configCc->getTitle();
94
    }
95
96
    /**
97
     * Url For Tokenize.
98
     *
99
     * @var string
100
     */
101
    public function getUrlForTokenize()
102
    {
103
        return $this->getUrl('payment/order/NumberToken');
104
    }
105
106
    /**
107
     * Use tax document capture - Cc.
108
     *
109
     * @var bool
110
     */
111
    public function getTaxDocumentCapture()
112
    {
113
        return $this->configCc->hasUseTaxDocumentCapture();
114
    }
115
116
    /**
117
     * Use phone capture - Cc.
118
     *
119
     * @var bool
120
     */
121
    public function getPhoneCapture()
122
    {
123
        return $this->configCc->hasUsePhoneCapture();
124
    }
125
126
    /**
127
     * Select Installment - Cc.
128
     *
129
     * @return array
130
     */
131
    public function getSelectInstallments(): array
132
    {
133
        $total = $this->session->getQuote()->getGrandTotal();
134
        $installments = $this->getInstallments($total);
135
136
        return $installments;
137
    }
138
139
    /**
140
     * Installments - Cc.
141
     *
142
     * @param float $amount
143
     *
144
     * @return array
145
     */
146
    public function getInstallments($amount): array
147
    {
148
        $interestByInst = $this->configCc->getInfoInterest();
149
        $plotlist = [];
150
        foreach ($interestByInst as $key => $interest) {
151
            if ($key > 0) {
152
                $plotValue = $this->getInterestSimple($amount, $interest, $key);
153
                $plotValue = number_format((float) $plotValue, 2, '.', '');
154
                $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

154
                $installmentPrice = $this->priceHelper->currency(/** @scrutinizer ignore-type */ $plotValue, true, false);
Loading history...
155
                $plotlist[$key] = $key.__('x of ').$installmentPrice;
156
            }
157
        }
158
159
        return $plotlist;
160
    }
161
162
    /**
163
     * Interest Simple - Cc.
164
     *
165
     * @param float $total
166
     * @param float $interest
167
     * @param int   $portion
168
     *
169
     * @return float
170
     */
171
    public function getInterestSimple($total, $interest, $portion): float
172
    {
173
        if ($interest) {
174
            $taxa = $interest / 100;
175
            $valinterest = $total * $taxa;
176
177
            return ($total + $valinterest) / $portion;
178
        }
179
180
        return $total / $portion;
181
    }
182
}
183