Passed
Push — main ( 1ecd49...db4bc4 )
by Osvaldo
05:56
created

VerificarPorRol::existeElRol()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace src;
4
5
use src\excepciones\ElRolNoExisteEnLosDatosDeVerificacionException;
6
use src\excepciones\LosDatosParaVerificarAutorizacionEstanVaciosException;
7
use src\excepciones\NoTieneAutorizacionException;
8
use src\interfaces\VerificarPermisosInterface;
9
10
class VerificarPorRol implements VerificarPermisosInterface
11
{
12
    private array $_datos;
13
14 11
    public function __construct(array $datosPorRol)
15
    {
16 11
        $this->_datos = $this->setDatos($datosPorRol);
17 11
    }
18
19 11
    public function verificar(string $rol, string $recursoSolicitado): void
20
    {
21 11
        $this->existeElRol($rol);
22
23 11
        if (!in_array($recursoSolicitado, $this->_datos[$rol])) {
24 1
            throw new NoTieneAutorizacionException("No tiene autorización");
25
        }
26 11
    }
27
28 11
    public function existeElRol(string $rol): void
29
    {
30 11
        if (!array_key_exists($rol, $this->_datos)) {
31 1
            throw new ElRolNoExisteEnLosDatosDeVerificacionException("El rol no está especificado en los datos, no se puede verificar");
32
        }
33 11
    }
34
35 11
    private function setDatos(array $array): array
36
    {
37 11
        if (count($array) == 0) {
38 1
            throw new LosDatosParaVerificarAutorizacionEstanVaciosException("Los datos para verificar si existe autorización están vacíos", 1);
39
        }
40
41 11
        return $array;
42
    }
43
}
44