Passed
Branch [email protected] (8a736f)
by Bruno
13:13
created

SubTotalAmountDataRequest::build()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 29
rs 9.7998
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\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...
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 Subtotal Amount Data Request - Payment amount structure.
20
 */
21
class SubTotalAmountDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Amount block name.
25
     */
26
    public const AMOUNT = 'amount';
27
28
    /**
29
     * @var SubjectReader
30
     */
31
    protected $subjectReader;
32
33
    /**
34
     * @var OrderAdapterFactory
35
     */
36
    protected $orderAdapterFactory;
37
38
    /**
39
     * @var Config
40
     */
41
    protected $config;
42
43
    /**
44
     * @param SubjectReader       $subjectReader
45
     * @param OrderAdapterFactory $orderAdapterFactory
46
     * @param Config              $config
47
     */
48
    public function __construct(
49
        SubjectReader $subjectReader,
50
        OrderAdapterFactory $orderAdapterFactory,
51
        Config $config
52
    ) {
53
        $this->subjectReader = $subjectReader;
54
        $this->orderAdapterFactory = $orderAdapterFactory;
55
        $this->config = $config;
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
        $payment = $paymentDO->getPayment();
78
79
        /** @var OrderAdapterFactory $orderAdapter * */
80
        $orderAdapter = $this->orderAdapterFactory->create(
81
            ['order' => $payment->getOrder()]
82
        );
83
84
        $grandTotal = $order->getGrandTotalAmount();
85
        $tax = $orderAdapter->getTaxAmount();
86
87
        $total = $grandTotal - $tax;
88
89
        $result[self::AMOUNT] = $this->config->formatPrice($total);
0 ignored issues
show
Bug introduced by
$total of type double is incompatible with the type integer expected by parameter $amount of Getnet\PaymentMagento\Ga...g\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

89
        $result[self::AMOUNT] = $this->config->formatPrice(/** @scrutinizer ignore-type */ $total);
Loading history...
90
91
        return $result;
92
    }
93
}
94