ReglaTasaCuota   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 139
ccs 46
cts 48
cp 0.9583
rs 10
wmc 23

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 35 10
A traslado() 0 3 1
A maximo() 0 3 1
A tipo() 0 3 1
A valor() 0 3 1
A impuesto() 0 3 1
A minimo() 0 3 1
A factor() 0 3 1
A retencion() 0 3 1
A valorIsValid() 0 21 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogos\CFDI;
6
7
use LogicException;
8
use PhpCfdi\SatCatalogos\Common\EntryWithVigencias;
9
use PhpCfdi\SatCatalogos\Common\EntryWithVigenciasTrait;
10
use PhpCfdi\SatCatalogos\Exceptions\SatCatalogosLogicException;
11
12
class ReglaTasaCuota implements EntryWithVigencias
13
{
14
    use EntryWithVigenciasTrait;
15
16
    public const TIPO_FIJO = 'Fijo';
17
18
    public const TIPO_RANGO = 'Rango';
19
20
    /** @var string */
21
    private $tipo;
22
23
    /** @var string */
24
    private $impuesto;
25
26
    /** @var string */
27
    private $factor;
28
29
    /** @var bool */
30
    private $traslado;
31
32
    /** @var bool */
33
    private $retencion;
34
35
    /** @var string */
36
    private $minimo;
37
38
    /** @var string */
39
    private $valor;
40
41
    /**
42
     * ReglaTasaCuota constructor.
43
     * @param string $tipo
44
     * @param string $impuesto
45
     * @param string $factor
46
     * @param bool $traslado
47
     * @param bool $retencion
48
     * @param string $minimo
49
     * @param string $valor Valor fijo o bien el máximo del rango
50
     * @param int $vigenteDesde
51
     * @param int $vigenteHasta
52
     */
53 17
    public function __construct(
54
        string $tipo,
55
        string $impuesto,
56
        string $factor,
57
        bool $traslado,
58
        bool $retencion,
59
        string $minimo,
60
        string $valor,
61
        int $vigenteDesde,
62
        int $vigenteHasta
63
    ) {
64 17
        if (self::TIPO_FIJO !== $tipo && self::TIPO_RANGO !== $tipo) {
65 2
            throw new SatCatalogosLogicException('El campo tipo no tiene uno de los valores permitidos');
66
        }
67
        if (
68 15
            ReglasTasaCuota::IMPUESTO_IEPS !== $impuesto
69 15
            && ReglasTasaCuota::IMPUESTO_IVA !== $impuesto
70 15
            && ReglasTasaCuota::IMPUESTO_ISR !== $impuesto
71
        ) {
72 2
            throw new SatCatalogosLogicException('El campo impuesto no tiene uno de los valores permitidos');
73
        }
74 13
        if (ReglasTasaCuota::FACTOR_CUOTA !== $factor && ReglasTasaCuota::FACTOR_TASA !== $factor) {
75 2
            throw new SatCatalogosLogicException('El campo factor no tiene uno de los valores permitidos');
76
        }
77 11
        if (! $traslado && ! $retencion) {
78 1
            throw new SatCatalogosLogicException('Los campos retención y traslado no pueden ser falsos ambos');
79
        }
80 10
        $this->tipo = $tipo;
81 10
        $this->impuesto = $impuesto;
82 10
        $this->factor = $factor;
83 10
        $this->traslado = $traslado;
84 10
        $this->retencion = $retencion;
85 10
        $this->minimo = $minimo;
86 10
        $this->valor = $valor;
87 10
        $this->setUpVigencias($vigenteDesde, $vigenteHasta);
88
    }
89
90 2
    public function tipo(): string
91
    {
92 2
        return $this->tipo;
93
    }
94
95 1
    public function impuesto(): string
96
    {
97 1
        return $this->impuesto;
98
    }
99
100 1
    public function factor(): string
101
    {
102 1
        return $this->factor;
103
    }
104
105 3
    public function traslado(): bool
106
    {
107 3
        return $this->traslado;
108
    }
109
110 3
    public function retencion(): bool
111
    {
112 3
        return $this->retencion;
113
    }
114
115 1
    public function minimo(): string
116
    {
117 1
        return $this->minimo;
118
    }
119
120 1
    public function maximo(): string
121
    {
122 1
        return $this->valor;
123
    }
124
125 1
    public function valor(): string
126
    {
127 1
        return $this->valor;
128
    }
129
130 7
    public function valorIsValid(string $valor): bool
131
    {
132 7
        if (! (bool) preg_match('/^\d{0,2}(\.\d{0,6})?$/', $valor)) {
133 1
            return false;
134
        }
135
136 7
        if (self::TIPO_FIJO === $this->tipo) {
137 5
            return $valor === $this->valor;
138
        }
139
140 2
        if (self::TIPO_RANGO === $this->tipo) {
141 2
            $delta = 1000000;
142 2
            $current = intval($delta * floatval($valor));
143 2
            $min = intval($delta * floatval($this->minimo));
144 2
            $max = intval($delta * floatval($this->valor));
145 2
            return ($current >= $min && $current <= $max);
146
        }
147
148
        /** @codeCoverageIgnore This is a safeguard since the object cannot be constructed with other type */
149
        throw new LogicException(
150
            "Don't know how to compare the current rule, it is not TIPO_FIJO or TIPO_RANGO",
151
        );
152
    }
153
}
154