Completed
Push — master ( b1c5c6...4dad7a )
by Christopher
12:41
created

Model::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 1
Metric Value
c 3
b 3
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Vundi\Potato;
4
5
use Vundi\Potato\Exceptions\IDShouldBeNumber;
6
7
class Model
8
{
9
    private static $db;
10
    public $db_fields = [];
11
    public static $ID;
12
    protected static $child_class;
13
14
    public function __construct()
15
    {
16
        self::$db = new Database();
17
        self::$child_class = get_called_class();
18
    }
19
20
    /**
21
     * Will make it possible to assign key value pairs dynamically
22
     * in the child class
23
     */
24
    public function __set($key, $value)
25
    {
26
        $this->db_fields[$key] = $value;
27
    }
28
29
    /**
30
     * Save a record in the table
31
     * calls the insert method in the Database class
32
     */
33
    public function save()
34
    {
35
        $s = new static();
36
37
        return self::$db->insert($s::$entity_table, $this->db_fields);
38
    }
39
40
    /**
41
     * Update a record in the table
42
     */
43
    public function update()
44
    {
45
        $s = new static();
46
47
        $where = "id = {$s::$ID}";
48
49
        self::$db->update($s::$entity_table, $this->db_fields, $where);
50
    }
51
52
    /**
53
     * Remove a record from the table with the specified ID
54
     * @param  in $id ID of record you want to remove
55
     */
56
    public static function remove($id)
57
    {
58
        $s = new static();
59
60
        if (is_int($id)) {
61
            $where = "id = {$id}";
62
            self::$db->delete($s::$entity_table, $where);
63
        } else {
64
            throw new IDShouldBeNumber('Pass in an ID as the parameter, ID has to be a number', 1);
65
        }
66
    }
67
68
    /**
69
     * @param  int $id ID of the record to be retrieved
70
     * @return object  Instance of the Class
71
     */
72
    public static function find($id)
73
    {
74
        // var_dump(get_called_class());
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        // die();
76
        $s = new static();
77
78
        if (is_int($id)) {
79
            //Set the ID of the class instance returned to $id since during update we shall update the
80
            //record with ID that matches the id passed during find
81
            $s::$ID = $id;
82
83
            $where = "id = {$id}";
84
            self::$db->select($s::$entity_table, $where);
85
86
            return self::$db->singleObject(self::$child_class);
87
        } else {
88
            throw new IDShouldBeNumber('Find only takes a number as a parameter', 1);
89
        }
90
    }
91
92
    public static function findWhere($conditions = array(), $fields = '*', $order = '', $limit = null, $offset = '')
93
    {
94
        $s = new static();
95
        $where = '';
96
        foreach ($conditions as $key => $value) {
97
            if (is_string($value)) {
98
                $where .= ' '.$key.' ="'.$value.'"'.' &&';
99
            } else {
100
                $where .= ' '.$key.' = '.$value.' &&';
101
            }
102
        }
103
        $where = rtrim($where, '&');
104
        self::$db->select($s::$entity_table, $where, $fields, $order, $limit, $offset);
105
106
        return self::$db->objectSet(self::$child_class);
0 ignored issues
show
Unused Code introduced by
The call to Database::objectSet() has too many arguments starting with self::$child_class.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
    }
108
109
    // public static function findWherein($query)
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
    // {
111
    //     $s = new static();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
    //     $results = self::$db->prepare($query);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
    //     $results->setFetchMode(PDO::FETCH_ASSOC);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
115
    //     return $results->fetch();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
    // }
117
118
    /**
119
     * Finds all records in the table
120
     * @return array
121
     */
122
    public static function findAll()
123
    {
124
        $s = new static();
125
126
        self::$db->select($s::$entity_table);
127
128
        return self::$db->objectSet();
129
    }
130
}
131