Passed
Push[email protected] ( 7201bc...6c257b )
by Bruno
03:40
created

ValidationBr::addRuleValidation()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 30
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 44
rs 6.9666

How to fix   Complexity   

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 O2TI\TaxDocumentValidationBr\Model;
10
11
use O2TI\TaxDocumentValidationBr\Helper\Config;
12
13
/**
14
 *  ValidationBr - Add Validation Brazilian for Tax Document.
15
 */
16
class ValidationBr
17
{
18
    /**
19
     * @var Config
20
     */
21
    protected $config;
22
23
    /**
24
     * @param Config $config
25
     */
26
    public function __construct(
27
        Config $config
28
    ) {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Change Rule Validation.
34
     *
35
     * @param array $fields
36
     *
37
     * @return array
38
     */
39
    public function addRuleValidation(array $fields): array
40
    {
41
        foreach ($fields as $key => $data) {
42
            if ($key === 'vat_id') {
43
                if ($this->config->getConfigByVatId('enabled_cpf') && $this->config->getConfigByVatId('enabled_cnpj')) {
44
                    $fields[$key]['validation'] = [
45
                        'required-entry'        => 1,
46
                        Config::VAT_CPF_OR_CNPJ => 1,
47
                    ];
48
                } elseif ($this->config->getConfigByVatId('enabled_cpf')) {
49
                    $fields[$key]['validation'] = [
50
                        'required-entry'     => 1,
51
                        Config::VAT_ONLY_CPF => 1,
52
                    ];
53
                } elseif ($this->config->getConfigByVatId('enabled_cnpj')) {
54
                    $fields[$key]['validation'] = [
55
                        'required-entry'      => 1,
56
                        Config::VAT_ONLY_CNPJ => 1,
57
                    ];
58
                }
59
            } elseif ($key === 'taxvat') {
60
                if ($this->config->getConfigByTaxvat('enabled_cpf')
61
                    && $this->config->getConfigByTaxvat('enabled_cnpj')
62
                ) {
63
                    $fields[$key]['validation'] = [
64
                        'required-entry'        => 1,
65
                        Config::VAT_CPF_OR_CNPJ => 1,
66
                    ];
67
                } elseif ($this->config->getConfigByTaxvat('enabled_cpf')) {
68
                    $fields[$key]['validation'] = [
69
                        'required-entry'     => 1,
70
                        Config::VAT_ONLY_CPF => 1,
71
                    ];
72
                } elseif ($this->config->getConfigByTaxvat('enabled_cnpj')) {
73
                    $fields[$key]['validation'] = [
74
                        'required-entry'      => 1,
75
                        Config::VAT_ONLY_CNPJ => 1,
76
                    ];
77
                }
78
            }
79
            continue;
80
        }
81
82
        return $fields;
83
    }
84
85
    /**
86
     * Add Tooltip.
87
     *
88
     * @param array $fields
89
     *
90
     * @return array
91
     */
92
    public function addTooltip(array $fields): array
93
    {
94
        foreach ($fields as $key => $data) {
95
            if ($key === 'vat_id') {
96
                if ($this->config->getConfigByVatId('enabled_cpf') && $this->config->getConfigByVatId('enabled_cnpj')) {
97
                    $fields[$key]['config']['tooltip'] = [
98
                        'description' => __('O CPF ou CNPJ é utilizado para envio e emissão de nota fiscal.'),
99
                    ];
100
                } elseif ($this->config->getConfigByVatId('enabled_cpf')) {
101
                    $fields[$key]['config']['tooltip'] = [
102
                        'description' => __('O CPF é utilizado para envio e emissão de nota fiscal.'),
103
                    ];
104
                } elseif ($this->config->getConfigByVatId('enabled_cnpj')) {
105
                    $fields[$key]['config']['tooltip'] = [
106
                        'description' => __('O CNPJ é utilizado para envio e emissão de nota fiscal.'),
107
                    ];
108
                }
109
            } elseif ($key === 'taxvat') {
110
                if ($this->config->getConfigByTaxvat('enabled_cpf')
111
                    && $this->config->getConfigByTaxvat('enabled_cnpj')
112
                ) {
113
                    $fields[$key]['config']['tooltip'] = [
114
                        'description' => __('O CPF ou CNPJ é utilizado para envio e emissão de nota fiscal.'),
115
                    ];
116
                } elseif ($this->config->getConfigByTaxvat('enabled_cpf')) {
117
                    $fields[$key]['config']['tooltip'] = [
118
                        'description' => __('O CPF é utilizado para envio e emissão de nota fiscal.'),
119
                    ];
120
                } elseif ($this->config->getConfigByTaxvat('enabled_cnpj')) {
121
                    $fields[$key]['config']['tooltip'] = [
122
                        'description' => __('O CNPJ é utilizado para envio e emissão de nota fiscal.'),
123
                    ];
124
                }
125
            }
126
            continue;
127
        }
128
129
        return $fields;
130
    }
131
}
132