Passed
Push[email protected] ( 6fef5b...356814 )
by Bruno
12:03 queued 10:11
created

SellerDataRequest::build()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 33
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 54
rs 8.7697

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 © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Request;
10
11
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Pricing\Helper\Data 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...
12
use Magento\Payment\Gateway\Request\BuilderInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Request\BuilderInterface 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 Moip\Magento2\Gateway\Config\Config;
14
use Moip\Magento2\Gateway\Config\ConfigCc;
15
use Moip\Magento2\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Da...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...
16
use Moip\Magento2\Gateway\SubjectReader;
17
18
/**
19
 * Class SellerDataRequest - Order seller amount structure.
20
 */
21
class SellerDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Receivers block name.
25
     */
26
    const RECEIVERS = 'receivers';
27
28
    /**
29
     * Moip Account block name.
30
     */
31
    const RECEIVERS_MOIP_ACCOUNT = 'moipAccount';
32
33
    /**
34
     * Moip Account Id block name.
35
     */
36
    const RECEIVERS_MOIP_ACCOUNT_ID = 'id';
37
38
    /**
39
     * Type Receiver block name.
40
     */
41
    const RECEIVERS_TYPE = 'type';
42
43
    /**
44
     * Secondary Type Receiver.
45
     * required.
46
     */
47
    const RECEIVERS_TYPE_SECONDARY = 'SECONDARY';
48
49
    /**
50
     * Amount Receiver block name.
51
     */
52
    const RECEIVERS_AMOUNT = 'amount';
53
54
    /**
55
     * Fixed Receiver Type block name.
56
     */
57
    const RECEIVERS_TYPE_FIXED = 'fixed';
58
59
    /**
60
     * Percent Receiver Type block name.
61
     */
62
    const RECEIVERS_TYPE_PERCENT = 'percent';
63
64
    /**
65
     * @var SubjectReader
66
     */
67
    private $subjectReader;
68
69
    /**
70
     * @var OrderAdapterFactory
71
     */
72
    private $orderAdapterFactory;
73
74
    /**
75
     * @var Config
76
     */
77
    private $config;
78
79
    /**
80
     * @var configCc
81
     */
82
    private $configCc;
83
84
    /**
85
     * @var priceHelper
86
     */
87
    private $priceHelper;
88
89
    /**
90
     * @param SubjectReader       $subjectReader
91
     * @param OrderAdapterFactory $orderAdapterFactory
92
     * @param Config              $Config
93
     * @param ConfigCc            $ConfigCc
94
     * @param CheckoutHelper      $checkoutHelper
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Request\CheckoutHelper 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...
95
     */
96
    public function __construct(
97
        SubjectReader $subjectReader,
98
        OrderAdapterFactory $orderAdapterFactory,
99
        Config $config,
100
        ConfigCc $configCc,
101
        PriceHelper $checkoutHelper
102
    ) {
103
        $this->subjectReader = $subjectReader;
104
        $this->orderAdapterFactory = $orderAdapterFactory;
105
        $this->config = $config;
106
        $this->configCc = $configCc;
107
        $this->priceHelper = $checkoutHelper;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function build(array $buildSubject)
114
    {
115
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
116
        $payment = $paymentDO->getPayment();
117
118
        $result = [];
119
120
        $orderAdapter = $this->orderAdapterFactory->create(
121
            ['order' => $payment->getOrder()]
122
        );
123
124
        $order = $paymentDO->getOrder();
125
126
        $storeId = $order->getStoreId();
127
        $useSplit = $this->config->getSplitValue('use_split', $storeId);
128
        if (!$useSplit) {
129
            return $result;
130
        }
131
132
        $addition = $orderAdapter->getTaxAmount();
0 ignored issues
show
Unused Code introduced by
The assignment to $addition is dead and can be removed.
Loading history...
133
        $interest = $orderAdapter->getBaseMoipInterestAmount();
134
        $grandTotal = $order->getGrandTotalAmount();
135
        $total = $grandTotal;
136
137
        $secondaryMPA = $this->config->getSplitValue('secondary_mpa', $storeId);
138
        $secondaryPercent = $this->config->getSplitValue('secondary_percent', $storeId);
139
        $commiUseShipping = $this->config->getSplitValue('secondary_percent_include_shipping', $storeId);
140
        $commiUseInterest = $this->config->getSplitValue('secondary_percent_include_interest', $storeId);
141
142
        if (!$commiUseInterest) {
143
            if($interest > 0) {
144
                $total = $grandTotal - $interest;  
145
            } elseif ($interest < 0) {
146
                $total = $grandTotal + $interest;
147
            }
148
        }
149
150
        if (!$commiUseShipping) {
151
            $total = $total - $orderAdapter->getShippingAmount();
152
        }
153
154
        $commission = $total * ($secondaryPercent / 100);
155
156
        $result[self::RECEIVERS][] = [
157
            self::RECEIVERS_MOIP_ACCOUNT => [
158
                self::RECEIVERS_MOIP_ACCOUNT_ID => $secondaryMPA,
159
            ],
160
            self::RECEIVERS_TYPE   => self::RECEIVERS_TYPE_SECONDARY,
161
            self::RECEIVERS_AMOUNT => [
162
                self::RECEIVERS_TYPE_FIXED => $this->config->formatPrice(round($commission, 2)),
0 ignored issues
show
Bug introduced by
round($commission, 2) of type double is incompatible with the type integer expected by parameter $amount of Moip\Magento2\Gateway\Config\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

162
                self::RECEIVERS_TYPE_FIXED => $this->config->formatPrice(/** @scrutinizer ignore-type */ round($commission, 2)),
Loading history...
163
            ],
164
        ];
165
166
        return $result;
167
    }
168
}
169