aroundBuild()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 25
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 51
rs 9.2088

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 © O2TI. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Getnet\SplitExampleMagento\Plugin\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\Request\BoletoPaymentDataRequest;
14
use Getnet\PaymentMagento\Gateway\SubjectReader;
15
use Getnet\SplitExampleMagento\Helper\Data as SplitHelper;
16
use Magento\Framework\App\Config\ScopeConfigInterface;
17
18
/**
19
 * Class Addtional Data Boleto Payment Data Request - add marketplace data in Transaction.
20
 */
21
class AddtionalDataBoletoPaymentDataRequest
22
{
23
    public const GUARANTOR_DOCUMENT_TYPE = 'guarantor_document_type';
24
    public const GUARANTOR_DOCUMENT_NUMBER = 'guarantor_document_number';
25
    public const GUARANTOR_NAME = 'guarantor_name';
26
27
    /**
28
     * @var SubjectReader
29
     */
30
    protected $subjectReader;
31
32
    /**
33
     * @var OrderAdapterFactory
34
     */
35
    protected $orderAdapterFactory;
36
37
    /**
38
     * @var Config
39
     */
40
    protected $config;
41
42
    /**
43
     * @var SplitHelper;
44
     */
45
    protected $splitHelper;
46
    /**
47
     * @var ScopeConfigInterface
48
     */
49
    protected $scopeConfig;
50
51
    /**
52
     * @param SubjectReader        $subjectReader
53
     * @param OrderAdapterFactory  $orderAdapterFactory
54
     * @param Config               $config
55
     * @param SplitHelper          $splitHelper
56
     * @param ScopeConfigInterface $scopeConfig
57
     */
58
    public function __construct(
59
        SubjectReader $subjectReader,
60
        OrderAdapterFactory $orderAdapterFactory,
61
        Config $config,
62
        SplitHelper $splitHelper,
63
        ScopeConfigInterface $scopeConfig
64
    ) {
65
        $this->subjectReader = $subjectReader;
66
        $this->orderAdapterFactory = $orderAdapterFactory;
67
        $this->config = $config;
68
        $this->splitHelper = $splitHelper;
69
        $this->scopeConfig = $scopeConfig;
70
    }
71
72
    /**
73
     * Around method Build.
74
     *
75
     * @param BoletoPaymentDataRequest $subject
76
     * @param \Closure                 $proceed
77
     * @param array                    $buildSubject
78
     *
79
     * @return mixin
0 ignored issues
show
Bug introduced by
The type Getnet\SplitExampleMagen...o\Gateway\Request\mixin 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...
80
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
81
     */
82
    public function aroundBuild(
83
        BoletoPaymentDataRequest $subject,
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed. ( Ignorable by Annotation )

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

83
        /** @scrutinizer ignore-unused */ BoletoPaymentDataRequest $subject,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
        \Closure $proceed,
85
        $buildSubject
86
    ) {
87
        $result = $proceed($buildSubject);
88
89
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
90
91
        $order = $paymentDO->getOrder();
92
93
        $storeId = $order->getStoreId();
94
95
        $items = $order->getItems();
96
97
        $availlable = false;
98
99
        foreach ($items as $item) {
100
            if ($item->getProduct()->getGetnetSubSellerId()) {
101
                $availlable = true;
102
            }
103
        }
104
105
        if (!$availlable) {
106
            return $result;
107
        }
108
109
        $typeDocument = 'CPF';
110
111
        $name = $this->splitHelper->getAdditionalGuarantorName($storeId);
112
113
        $document = $this->splitHelper->getAdditionalGuarantorNumber($storeId);
114
115
        $document = preg_replace('/[^0-9]/', '', $document);
116
117
        if (strlen($document) === 14) {
118
            $typeDocument = 'CNPJ';
119
        }
120
121
        $addtionalData = [
122
            self::GUARANTOR_DOCUMENT_TYPE   => $typeDocument,
123
            self::GUARANTOR_DOCUMENT_NUMBER => $document,
124
            self::GUARANTOR_NAME            => $name,
125
        ];
126
127
        $result[BoletoPaymentDataRequest::METHOD] = array_merge(
128
            $result[BoletoPaymentDataRequest::METHOD],
129
            $addtionalData
130
        );
131
132
        return $result;
133
    }
134
}
135