Passed
Push — main ( c1cea3...1d91de )
by Osvaldo
13:30
created

JoinWhereOrderByLimit::crear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 25
rs 9.7333
1
<?php
2
namespace src\factory;
3
4
use src\FactoryClassInterface;
5
use src\pdodatabase\conexion\CrearConexionBaseDeDatos;
6
use src\pdodatabase\consultas\select\ConsultaJoinWhereOrdenYLimite;
7
use src\pdodatabase\ejecutar\EjecutarConsultaConDatos;
8
use src\pdodatabase\elementos\Campos;
9
use src\pdodatabase\elementos\Join as ElementosJoin;
10
use src\pdodatabase\elementos\Joins;
11
use src\pdodatabase\elementos\Limite;
12
use src\pdodatabase\elementos\NombreColumnaJoin;
13
use src\pdodatabase\elementos\Orden;
14
use src\pdodatabase\elementos\OrdenYLimite;
15
use src\pdodatabase\elementos\Tabla;
16
use src\pdodatabase\elementos\TipoDeJoin;
17
use src\pdodatabase\elementos\ValidadorDeParametrosWhere;
18
use src\pdodatabase\elementos\Where;
19
use src\pdodatabase\sentencias\select\SentenciaJoinWhereOrdenOLimite;
20
use src\pdodatabase\sentencias\select\SentenciaJoinWhereOrdenYLimite;
21
22
class JoinWhereOrderByLimit implements FactoryClassInterface
23
{
24
    private $_joins = [];
25
26
    public function crear(array $array): ConsultaJoinWhereOrdenYLimite
27
    {
28
        $conexion = new CrearConexionBaseDeDatos;
29
        $conexion = $conexion->crear([]);
30
31
        $sentencia = new SentenciaJoinWhereOrdenYLimite(
32
            new Joins(
33
                new Tabla($array['tabla']),
34
                new Campos($array['campos']),
35
                $this->obtenerJoins($array)
36
            ),
37
            new Where(
38
                new ValidadorDeParametrosWhere($array['where'])
39
            ),
40
            new OrdenYLimite(
41
                new Orden($array['order']),
42
                new Limite($array['limit'])
43
            )
44
        );
45
        
46
        return new ConsultaJoinWhereOrdenYLimite(
47
            new EjecutarConsultaConDatos(
48
                $conexion
49
            ),
50
            $sentencia
51
        );
52
    }
53
54
    private function obtenerJoins(array $array): array
55
    {
56
        foreach ($array['join'] as $value)
57
        {
58
            array_push($this->_joins, $this->crearJoin($value, $array['tabla']));           
59
60
            $this->multilplesJoin($value, $value['tabla']);
61
        }
62
63
        return $this->_joins;
64
    }
65
66
    private function crearJoin(array $array, string $tablaPadre): ElementosJoin
67
    {
68
        return new ElementosJoin(
69
            new TipoDeJoin($array['tipo']),
70
            new Tabla($tablaPadre),
71
            new Tabla($array['tabla']),
72
            new Campos($array['campos']),
73
            new NombreColumnaJoin($array['key'])
74
        );
75
    }
76
77
    private function multilplesJoin(array $value, string $tablaPadre): void
78
    {
79
        if(isset($value['join']))
80
        {
81
            foreach ($value['join'] as $value)
82
            {
83
               array_push($this->_joins, $this->crearJoin($value, $tablaPadre));
84
            }
85
86
            $this->multilplesJoin($value, $value['tabla']);
87
        }
88
    }
89
}