Passed
Push — main ( 80bde2...4e6f22 )
by Osvaldo
05:18
created

Insert::datos()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace src\pdodatabase\elementos;
3
4
use Exception;
5
use src\interfaces\InsertInterface;
6
7
class Insert implements InsertInterface
8
{
9
    private $_where;
10
11 7
    public function __construct(array $datos)
12
    {
13 7
        $this->_where = $this->setDatos($datos);
14 6
    }
15
16 3
    public function sql(): string
17
    {
18 3
        return $this->armarConsulta($this->_where);
19
    }
20
21 3
    public function datos(): array
22
    {
23 3
        return $this->_where;
24
    }
25
26 7
    private function setDatos(array $array): array
27
    {
28 7
        $this->estaVacio($array);
29 6
        $this->columnaVacia($array);
30
31 6
        return $array;
32
    }
33
34 7
    private function estaVacio(array $array): void
35
    {
36 7
        if (count($array) == 0)
37
        {
38 1
            throw new Exception("Error Processing Request");
39
        }
40 6
    }
41
42 6
    private function columnaVacia(array $array): void
43
    {
44 6
        foreach ($array as $key => $value)
45
        {
46 6
            if(empty($key))
47
            {
48
                throw new Exception("Error Processing Request");   
49
            }
50
        }
51 6
    }
52
53 3
    private function armarConsulta(array $array): string
54
    {
55 3
        $keys = array_keys($array);
56 3
        $values = '';
57 3
        $x=1;
58
59 3
        foreach($array as $field)
60
        {
61 3
            $values.= "?";
62
63 3
            if($x < count($array))
64
            {
65 1
                $values.= ', ';
66
            }
67
68 3
            $x++;
69
        }
70
71 3
        return "(`". implode('`, `', $keys) ."`) VALUES ({$values})";
72
    }
73
}