AmountDenyDataRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 33 5
A __construct() 0 8 1
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\SubjectReader;
14
use InvalidArgumentException;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Request\BuilderInterface;
17
18
/**
19
 * Class Amount Deny Data Request - Payment amount structure.
20
 */
21
class AmountDenyDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Amount block name.
25
     */
26
    public const CANCEL_AMOUNT = 'cancel_amount';
27
28
    /**
29
     * @var SubjectReader
30
     */
31
    protected $subjectReader;
32
33
    /**
34
     * @var Config
35
     */
36
    protected $config;
37
38
    /**
39
     * @var ConfigCc
40
     */
41
    protected $configCc;
42
43
    /**
44
     * @param SubjectReader $subjectReader
45
     * @param Config        $config
46
     * @param ConfigCc      $configCc
47
     */
48
    public function __construct(
49
        SubjectReader $subjectReader,
50
        Config $config,
51
        ConfigCc $configCc
52
    ) {
53
        $this->subjectReader = $subjectReader;
54
        $this->config = $config;
55
        $this->configCc = $configCc;
56
    }
57
58
    /**
59
     * Build.
60
     *
61
     * @param array $buildSubject
62
     */
63
    public function build(array $buildSubject)
64
    {
65
        if (!isset($buildSubject['payment'])
66
        || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
67
        ) {
68
            throw new InvalidArgumentException('Payment data object should be provided');
69
        }
70
71
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
72
73
        $result = [];
74
75
        $order = $paymentDO->getOrder();
76
77
        $grandTotal = $order->getGrandTotalAmount();
78
79
        $payment = $paymentDO->getPayment();
80
81
        $installment = $payment->getAdditionalInformation('cc_installments') ?: 1;
82
83
        $result[self::CANCEL_AMOUNT] = $this->config->formatPrice($grandTotal);
84
85
        if ($installment > 1) {
86
            $order = $paymentDO->getOrder();
87
            $storeId = $order->getStoreId();
88
            $amountInterest = $this->configCc->getInterestToAmount($installment, $grandTotal, $storeId);
89
            $total = $grandTotal + $amountInterest;
90
            $result = [
91
                self::CANCEL_AMOUNT     => $this->config->formatPrice($total),
92
            ];
93
        }
94
95
        return $result;
96
    }
97
}
98