Completed
Push — master ( df362d...2da97c )
by Magnus
01:52
created

ActiveRecordModel   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 51.22%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 227
ccs 42
cts 82
cp 0.5122
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setDb() 0 4 1
A checkDb() 0 6 2
A findAllWhere() 0 11 2
A update() 0 15 3
A delete() 0 13 3
A getProperties() 0 8 1
A find() 0 10 1
A findAllSql() 0 7 1
A save() 0 10 3
A create() 0 14 1
A findAll() 0 9 1
1
<?php
2
3
namespace Anax\Database;
4
5
use \Anax\Database\DatabaseQueryBuilder;
6
use \Anax\Database\Exception\ActiveRecordException;
7
8
/**
9
 * An implementation of the Active Record pattern to be used as
10
 * base class for database driven models.
11
 */
12
class ActiveRecordModel
13
{
14
    /**
15
     * @var DatabaseQueryBuilder $db the object for persistent
16
     *                               storage.
17
     */
18
    protected $db = null;
19
20
    /**
21
     * @var string $tableName name of the database table.
22
     */
23
    protected $tableName = null;
24
25
26
27
    /**
28
     * Set the database object to use for accessing storage.
29
     *
30
     * @param DatabaseQueryBuilder $db as database access object.
31
     *
32
     * @return void
33
     */
34 3
    public function setDb(DatabaseQueryBuilder $db)
35
    {
36 3
        $this->db = $db;
37 3
    }
38
39
40
41
    /**
42
     * Check if database is injected or throw an exception.
43
     *
44
     * @throws ActiveRecordException when database is not set.
45
     *
46
     * @return void
47
     */
48 3
    protected function checkDb()
49
    {
50 3
        if (!$this->db) {
51
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
52
        }
53 3
    }
54
55
56
57
    /**
58
     * Get essential object properties.
59
     *
60
     * @return array with object properties.
61
     */
62 3
    protected function getProperties()
63
    {
64 3
        $properties = get_object_vars($this);
65 3
        unset($properties['tableName']);
66 3
        unset($properties['db']);
67 3
        unset($properties['di']);
68 3
        return $properties;
69
    }
70
71
72
73
    /**
74
     * Find and return first object found by search criteria and use
75
     * its data to populate this instance.
76
     *
77
     * @param string $column to use in where statement.
78
     * @param mixed  $value  to use in where statement.
79
     *
80
     * @return this
81
     */
82 2
    public function find($column, $value)
83
    {
84 2
        $this->checkDb();
85 2
        return $this->db->connect()
86 2
                        ->select()
87 2
                        ->from($this->tableName)
88 2
                        ->where("$column = ?")
89 2
                        ->execute([$value])
0 ignored issues
show
Documentation introduced by
array($value) is of type array<integer,*,{"0":"*"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90 2
                        ->fetchInto($this);
91
    }
92
93
94
95
    /**
96
     * Find and return all.
97
     *
98
     * @return array
99
     */
100 1
    public function findAll()
101
    {
102 1
        $this->checkDb();
103 1
        return $this->db->connect()
104 1
                        ->select()
105 1
                        ->from($this->tableName)
106 1
                        ->execute()
107 1
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
Documentation introduced by
get_class($this) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
    }
109
110
111
112
    /**
113
     * Find and return all matching a search criteria of
114
     * for example `id = ?` or `id IN [?, ?]`.
115
     *
116
     * @param string $where to use in where statement.
117
     * @param mixed  $value to use in where statement.
118
     *
119
     * @return array of object of this class
120
     */
121
    public function findAllWhere($where, $value)
122
    {
123
        $this->checkDb();
124
        $params = is_array($value) ? $value : [$value];
125
        return $this->db->connect()
126
                        ->select()
127
                        ->from($this->tableName)
128
                        ->where($where)
129
                        ->execute($params)
0 ignored issues
show
Documentation introduced by
$params is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
Documentation introduced by
get_class($this) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
    }
132
133
134
    /**
135
     * Execute rawsql
136
     * @param string $sql rawsql
137
     * @param array $params params
138
     *
139
     * @return array
140
     */
141
    public function findAllSql($sql, $params)
142
    {
143
        $this->checkDb();
144
        return $this->db->connect()
145
                        ->execute($sql, $params)
146
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
Documentation introduced by
get_class($this) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
    }
148
149
150
151
    /**
152
     * Save current object/row, insert if id is missing and do an
153
     * update if the id exists.
154
     *
155
     * @return void
156
     */
157 3
    public function save($idName = null, $id = null)
158
    {
159 3
        if (isset($this->id)) {
160
            return $this->update();
161 3
        } elseif ($idName !== null) {
162
            return $this->update($idName, $id);
163
        }
164
165 3
        return $this->create();
166
    }
167
168
169
170
    /**
171
     * Create new row.
172
     *
173
     * @return void
174
     */
175 3
    protected function create()
176
    {
177 3
        $this->checkDb();
178 3
        $properties = $this->getProperties();
179 3
        unset($properties['id']);
180 3
        $columns = array_keys($properties);
181 3
        $values  = array_values($properties);
182
183 3
        $this->db->connect()
184 3
                 ->insert($this->tableName, $columns)
185 3
                 ->execute($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<integer,?>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
187 3
        $this->id = $this->db->lastInsertId();
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
188 3
    }
189
190
191
192
    /**
193
     * Update row.
194
     *
195
     * @return void
196
     */
197
    protected function update($idName = null, $id = null)
198
    {
199
         $this->checkDb();
200
         $properties = $this->getProperties();
201
         unset($properties['id']);
202
         $columns = array_keys($properties);
203
         $values  = array_values($properties);
204
         $values[] = isset($this->id) ? $this->id : $id ;
205
         $setId = $idName !== null ? $idName : "id";
206
207
         $this->db->connect()
208
                  ->update($this->tableName, $columns)
209
                  ->where("$setId = ?")
210
                  ->execute($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<integer,?>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
211
    }
212
213
214
    
215
216
217
218
    /**
219
     * Delete row.
220
     *
221
     * @param integer $id to delete or use $this->id as default.
222
     *
223
     * @return void
224
     */
225
    public function delete($idName = null, $id = null)
226
    {
227
        $this->checkDb();
228
        $id = $id ?: $this->id;
229
        $setId = $idName !== null ? $idName : "id";
230
231
        $this->db->connect()
232
                 ->deleteFrom($this->tableName)
233
                 ->where("$setId = ?")
234
                 ->execute([$id]);
0 ignored issues
show
Documentation introduced by
array($id) is of type array<integer,?,{"0":"?"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
236
        $this->id = null;
237
    }
238
}
239