Controlador::controladorExiste()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
namespace src;
3
4
use Exception;
5
use const CONTROLADOR_DIR;
6
use const CONTROLADOR_POR_DEFECTO;
7
use const CONTROLADOR_NAMESPACE;
8
9
use src\Enlace;
10
11
class Controlador
12
{
13
    private $_controlador;
14
15
    public function __construct(Enlace $url)
16
    {
17
        $this->_controlador = $this->setControlador($url->enlace());
18
    }
19
20
    public function controlador(): string
21
    {
22
        return $this->_controlador;
23
    }
24
25
    private function setControlador(array $url): string
26
    {
27
        return $this->controladorExiste($this->controladorDeclarado($url));
28
    }
29
30
    //controlador esta declarado
31
    private function controladorDeclarado(array $url)
32
    {
33
        if (!isset($url[0])) {
34
            return CONTROLADOR_POR_DEFECTO;
35
        }
36
37
        return ucwords($url[0]);
38
    }
39
40
    //controlador existe
41
42
    private function controladorExiste(string $controlador): string
43
    {
44
        if (!file_exists(CONTROLADOR_DIR.$controlador.'.php') || !class_exists(CONTROLADOR_NAMESPACE.$controlador)) {
45
            return $this->controladorPorDefectoExiste();
46
        }
47
48
        return $controlador;
49
    }
50
51
    private function controladorPorDefectoExiste(): string
52
    {
53
        if (!file_exists(CONTROLADOR_DIR.CONTROLADOR_POR_DEFECTO.'.php') || !class_exists(CONTROLADOR_NAMESPACE.CONTROLADOR_POR_DEFECTO)) {
54
            throw new Exception("El controlador por defecto no existe");
55
        }
56
57
        return CONTROLADOR_POR_DEFECTO;
58
    }
59
}
60