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

JoinWhereBetweenLimit::obtenerJoins()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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