Passed
Push — main ( df4185...84fd6f )
by Osvaldo
12:01
created

Join::crearJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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