Model   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 91.84%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 92
ccs 45
cts 49
cp 0.9184
rs 10
c 0
b 0
f 0
wmc 19

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