Passed
Branch [email protected] (0b3c39)
by Bruno
03:01
created

TaxDocumentDataRequest::getValueForTaxDocument()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 31
rs 9.0777
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\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...
12
use Moip\Magento2\Gateway\Config\Config;
13
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...
14
use Moip\Magento2\Gateway\SubjectReader;
15
16
/**
17
 * Class TaxDocumentDataRequest - Fiscal document data structure.
18
 */
19
class TaxDocumentDataRequest implements BuilderInterface
20
{
21
    /**
22
     * BillingAddress block name.
23
     */
24
    const TAX_DOCUMENT = 'taxDocument';
25
26
    /**
27
     * The street address. Maximum 255 characters
28
     * Required.
29
     */
30
    const TAX_DOCUMENT_TYPE = 'type';
31
32
    /**
33
     * The street number. 1 or 10 alphanumeric digits
34
     * Required.
35
     */
36
    const TAX_DOCUMENT_NUMBER = 'number';
37
38
    /**
39
     * @var SubjectReader
40
     */
41
    private $subjectReader;
42
43
    /**
44
     * @var OrderAdapterFactory
45
     */
46
    private $orderAdapterFactory;
47
48
    /**
49
     * @var Config
50
     */
51
    private $config;
52
53
    /**
54
     * @param SubjectReader       $subjectReader
55
     * @param OrderAdapterFactory $orderAdapterFactory
56
     * @param Config              $config
57
     */
58
    public function __construct(
59
        SubjectReader $subjectReader,
60
        OrderAdapterFactory $orderAdapterFactory,
61
        Config $config
62
    ) {
63
        $this->subjectReader = $subjectReader;
64
        $this->orderAdapterFactory = $orderAdapterFactory;
65
        $this->config = $config;
66
    }
67
68
    /**
69
     * get Value For Tax Document.
70
     *
71
     * @param $orderAdapter
72
     *
73
     * @return value
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Request\value 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...
74
     */
75
    public function getValueForTaxDocument($orderAdapter)
76
    {
77
        $obtainTaxDocFrom = $this->config->getAddtionalValue('type_cpf');
78
79
        if ($obtainTaxDocFrom === 'customer') {
80
            $taxDocument = $orderAdapter->getCustomerTaxvat();
81
            $attTaxDocCus = $this->config->getAddtionalValue('cpf_for_customer');
82
83
            if ($attTaxDocCus !== 'taxvat') {
84
                $taxDocument = $orderAdapter->getData($attTaxDocCus);
85
            }
86
        } else {
87
            $taxDocument = $orderAdapter->getBillingAddress()->getVatId();
88
            $attTaxDocAddress = $this->config->getAddtionalValue('cpf_for_address');
89
90
            if ($attTaxDocAddress !== 'vat_id') {
91
                $taxDocument = $orderAdapter->getBillingAddress()->getData($attTaxDocAddress);
92
            }
93
        }
94
95
        // * Contigência para caso não haja o atributo informado pelo Admin busque me 2 outros campos comuns.
96
        if (!$taxDocument) {
97
            $taxDocument = $orderAdapter->getBillingAddress()->getVatId();
98
99
            $obtainTaxDocFrom = $this->config->getAddtionalValue('type_cpf');
100
            if ($obtainTaxDocFrom === 'customer') {
101
                $taxDocument = $orderAdapter->getCustomerTaxvat();
102
            }
103
        }
104
105
        return $taxDocument;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function build(array $buildSubject)
112
    {
113
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
114
        $payment = $paymentDO->getPayment();
115
        $taxDocument = null;
116
        $typeDocument = 'CPF';
117
        $result = [];
118
        $orderAdapter = $this->orderAdapterFactory->create(
119
            ['order' => $payment->getOrder()]
120
        );
121
        //* sobrescreve na order o documento caso seja capturado no formulario de pagamento
122
        if ($payment->getAdditionalInformation('cc_holder_tax_document')) {
123
            $taxDocument = $payment->getAdditionalInformation('cc_holder_tax_document');
124
        }
125
126
        if ($payment->getAdditionalInformation('boleto_payer_tax_document')) {
127
            $taxDocument = $payment->getAdditionalInformation('boleto_payer_tax_document');
128
        }
129
130
        if (!$taxDocument) {
131
            $taxDocument = $this->getValueForTaxDocument($orderAdapter);
132
        }
133
134
        $taxDocument = preg_replace('/[^0-9]/', '', $taxDocument);
135
136
        if (strlen($taxDocument) === 14) {
137
            $typeDocument = 'CNPJ';
138
        }
139
140
        if ($typeDocument) {
141
            $result[CustomerDataRequest::CUSTOMER][self::TAX_DOCUMENT] = [
142
                self::TAX_DOCUMENT_TYPE   => $typeDocument,
143
                self::TAX_DOCUMENT_NUMBER => $taxDocument,
144
            ];
145
        }
146
147
        return $result;
148
    }
149
}
150