Completed
Push — master ( fc1e37...3ba2dc )
by Edson
01:42
created

Model   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
eloc 35
dl 0
loc 85
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 11 2
A deleteAll() 0 3 1
A getTable() 0 4 1
A all() 0 4 2
A bind() 0 13 3
A find() 0 4 2
A getProperties() 0 7 2
A update() 0 10 1
A select() 0 5 2
A delete() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bonfim\ActiveRecord;
6
7
use PDO;
8
use PDOStatement;
9
use ReflectionClass;
10
11
use Bonfim\ActiveRecord\ActiveRecord as AR;
12
13
abstract class Model
14
{
15 9
    public static function all(): array
16
    {
17 9
        $consulta = AR::execute("SELECT * FROM `".self::getTable()."`");
18 9
        return is_null($consulta) ? [] : self::bind($consulta);
19
    }
20
21 12
    public static function find(string $where, array $values): array
22
    {
23 12
        $consulta = AR::execute("SELECT * FROM `".self::getTable()."` ".$where, $values);
24 12
        return is_null($consulta) ? [] : self::bind($consulta);
25
    }
26
27 6
    public static function select(array $keys, string $cond = '', array $values = []): array
28
    {
29 6
        $q = "SELECT ".implode(', ', $keys)." FROM ".self::getTable()." $cond";
30 6
        $consulta = AR::execute($q, $values);
31 6
        return is_null($consulta) ? [] : self::bind($consulta);
32
    }
33
34 3
    public function save(): int
35
    {
36 3
        $this->getProperties($keys, $values);
37
38 3
        $q = "INSERT INTO `".self::getTable()."` (`".implode('`, `', $keys)."`) VALUES (";
39 3
        for ($i = 0; $i < count($keys) - 1; $i++) {
40 3
            $q .= "?, ";
41
        }
42 3
        $q .= "?)";
43
44 3
        return AR::execute($q, $values)->rowCount();
45
    }
46
47 3
    public function update(): int
48
    {
49 3
        $this->getProperties($keys, $values);
50 3
        $values[] = $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Bonfim\ActiveRecord\Model. Did you maybe forget to declare it?
Loading history...
51
52 3
        $q = "UPDATE `".self::getTable()."` SET " . "`";
53 3
        $q .= implode('` = ?, `', $keys);
54 3
        $q .= "` = ? WHERE `id` = ?";
55
56 3
        return AR::execute($q, $values)->rowCount();
57
    }
58
59 3
    public function delete(): int
60
    {
61 3
        return AR::execute("DELETE FROM `".self::getTable()."` WHERE `id` = ?", [$this->id])->rowCount();
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Bonfim\ActiveRecord\Model. Did you maybe forget to declare it?
Loading history...
62
    }
63
64 3
    public static function deleteAll(): int
65
    {
66 3
        return AR::execute("DELETE FROM `".self::getTable()."`")->rowCount();
67
    }
68
69 30
    private static function getTable(): string
70
    {
71 30
        $class = new ReflectionClass(get_called_class());
72 30
        return Inflect::pluralize(strtolower($class->getShortName()));
73
    }
74
75 6
    private function getProperties(&$keys, &$values)
76
    {
77 6
        $properties = (new ReflectionClass(get_called_class()))->getProperties();
78
79 6
        foreach ($properties as $property) {
80 6
            $keys[]   = $property->name;
81 6
            $values[] = $this->{$property->name};
82
        }
83 6
    }
84
85 24
    private static function bind(PDOStatement $exec): array
86
    {
87 24
        $resp  = [];
88 24
        $class = get_called_class();
89
90 24
        foreach ($exec->fetchAll(PDO::FETCH_OBJ) as $i => $fetch) {
91 15
            $resp[$i] = new $class;
92 15
            foreach ($fetch as $k => $v) {
93 15
                $resp[$i]->$k = $v;
94
            }
95
        }
96
97 24
        return $resp;
98
    }
99
}
100