Passed
Push — main ( 25a2ce...9b261b )
by Osvaldo
05:11 queued 03:04
created

Pregunta::texto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
namespace src\preguntas;
3
4
use InvalidArgumentException;
5
use src\interfaces\PreguntaInterface;
6
7
class Pregunta 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
     * @var Consume
36
     */
37
    private Consume $_consume;
38
39
    /**
40
     *
41
     * @param string $valor
42
     * @param array $valoresPermitidos
43
     * @param Consume $consume
44
     */
45 10
    public function __construct(string $valor, array $valoresPermitidos, Consume $consume)
46
    {
47 10
        $this->_consume = $consume;
48 10
        $this->_valores = $valoresPermitidos;
49 10
        $this->_valor = $this->setValor($valor);
50 10
        $this->_texto = $this->valorTexto();
51 10
        $this->_numero = $this->valorNumero();
52 10
    }
53
54
    /**
55
     *
56
     * @return string
57
     */
58 1
    public function texto(): string
59
    {
60 1
        return $this->_texto;
61
    }
62
63
    /**
64
     *
65
     * @return string
66
     */
67 2
    public function numero(): string
68
    {
69 2
        return $this->_numero;
70
    }
71
    
72
    /**
73
     *
74
     * @return string
75
     */
76 10
    private function valorTexto(): string
77
    {
78 10
        if (is_numeric($this->_valor)) {
79 10
            return $this->_valores[$this->_valor];
80
        }
81
82 1
        return $this->_valor;
83
    }
84
85
    /**
86
     *
87
     * @return string
88
     */
89 10
    private function valorNumero(): string
90
    {
91 10
        if (!is_numeric($this->_valor)) {
92 1
            return (string) array_search($this->_valor, $this->_valores);
93
        }
94
95 10
        return $this->_valor;
96
    }
97
98
    /**
99
     * Define si lo que recibió la clase es el valor número aceptado o texto
100
     *
101
     * @param string $string
102
     *
103
     * @return string
104
     */
105 10
    private function setValor(string $string): string
106
    {
107 10
        if (!$this->_consume->consume()) {
108 10
            return '0';
109
        }
110
111 10
        if (array_key_exists($string, $this->_valores)) {
112 10
            return $string;
113
        }
114
115 2
        if (in_array($string, $this->_valores)) {
116 1
            return $string;
117
        }
118
119 1
        throw new InvalidArgumentException("El tipo de valor para la pregunta es incorrecto");
120
    }
121
}
122