PreguntaUno   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 101
ccs 24
cts 24
cp 1
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setValor() 0 11 3
A valorNumero() 0 7 2
A __construct() 0 6 1
A texto() 0 3 1
A valorTexto() 0 7 2
A numero() 0 3 1
1
<?php declare(strict_types=1);
2
namespace src\preguntas;
3
4
use InvalidArgumentException;
5
use src\interfaces\PreguntaInterface;
6
7
class PreguntaUno implements PreguntaInterface
8
{
9
    /**
10
     *
11
     * @var array
12
     */
13
    private array $_valores;
14
15
    /**
16
     *
17
     * @var string
18
     */
19
    private string $_valor;
20
    
21
    /**
22
     *
23
     * @var string
24
     */
25
    private string $_texto;
26
27
    /**
28
     *
29
     * @var string
30
     */
31
    private string $_numero;
32
33
34
    /**
35
     *
36
     * @param string $valor
37
     */
38 11
    public function __construct(string $valor, array $valoresPermitidos)
39
    {
40 11
        $this->_valores = $valoresPermitidos;
41 11
        $this->_valor = $this->setValor($valor);
42 11
        $this->_texto = $this->valorTexto();
43 11
        $this->_numero = $this->valorNumero();
44 11
    }
45
46
    /**
47
     *
48
     * @return string
49
     */
50 4
    public function texto(): string
51
    {
52 4
        return $this->_texto;
53
    }
54
55
    /**
56
     *
57
     * @return string
58
     */
59 11
    public function numero(): string
60
    {
61 11
        return $this->_numero;
62
    }
63
    
64
    /**
65
     *
66
     * @return string
67
     */
68 11
    private function valorTexto(): string
69
    {
70 11
        if (is_numeric($this->_valor)) {
71 11
            return $this->_valores[$this->_valor];
72
        }
73
74 1
        return $this->_valor;
75
    }
76
77
    /**
78
     *
79
     * @return string
80
     */
81 11
    private function valorNumero(): string
82
    {
83 11
        if (!is_numeric($this->_valor)) {
84 1
            return (string) array_search($this->_valor, $this->_valores);
85
        }
86
87 11
        return $this->_valor;
88
    }
89
90
    /**
91
     * Define si lo que recibió la clase es el valor número aceptado o texto
92
     *
93
     * @param string $string
94
     *
95
     * @return string
96
     */
97 11
    private function setValor(string $string): string
98
    {
99 11
        if (array_key_exists($string, $this->_valores)) {
100 11
            return $string;
101
        }
102
103 3
        if (in_array($string, $this->_valores)) {
104 1
            return $string;
105
        }
106
107 2
        throw new InvalidArgumentException("El tipo de valor para la pregunta es incorrecto");
108
    }
109
}
110