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

JoinOrderByLimit::multilplesJoin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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