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

Insert   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 65
ccs 29
cts 30
cp 0.9667
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A datos() 0 3 1
A armarConsulta() 0 19 3
A columnaVacia() 0 7 3
A sql() 0 3 1
A setDatos() 0 6 1
A __construct() 0 3 1
A estaVacio() 0 5 2
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
}