Completed
Push — master ( 287087...f3c7b7 )
by Mikael
01:46
created

ActiveRecordModel::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 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
     * @var string $tableIdColumn name of the id column in the database table.
27
     */
28
    protected $tableIdColumn = "id";
29
30
31
32
    /**
33
     * Set the database object to use for accessing storage.
34
     *
35
     * @param DatabaseQueryBuilder $db as database access object.
36
     *
37
     * @return void
38
     */
39 5
    public function setDb(DatabaseQueryBuilder $db)
40
    {
41 5
        $this->db = $db;
42 5
    }
43
44
45
46
    /**
47
     * Check if database is injected or throw an exception.
48
     *
49
     * @throws ActiveRecordException when database is not set.
50
     *
51
     * @return void
52
     */
53 6
    protected function checkDb()
54
    {
55 6
        if (!$this->db) {
56 1
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
57
        }
58 5
    }
59
60
61
62
    /**
63
     * Get essential object properties.
64
     *
65
     * @return array with object properties.
66
     */
67 5
    protected function getProperties()
68
    {
69 5
        $properties = get_object_vars($this);
70
        unset(
71 5
            $properties['tableName'],
72
            $properties['db'],
73
            $properties['di'],
74
            $properties['tableIdColumn']
75
        );
76 5
        return $properties;
77
    }
78
79
80
81
    /**
82
     * Find and return first object found by search criteria and use
83
     * its data to populate this instance.
84
     *
85
     * @param string $column to use in where statement.
86
     * @param mixed  $value  to use in where statement.
87
     *
88
     * @return this
89
     */
90 1
    public function find($column, $value)
91
    {
92 1
        return $this->findWhere("$column = ?", $value);
93
    }
94
95
96
97
    /**
98
     * Find and return first object by its tableIdColumn and use
99
     * its data to populate this instance.
100
     *
101
     * @param string $column to use in where statement.
0 ignored issues
show
Bug introduced by
There is no parameter named $column. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
102
     * @param mixed  $value  to use in where statement.
103
     *
104
     * @return this
105
     */
106 3
    public function findById($value)
107
    {
108 3
        return $this->findWhere("{$this->tableIdColumn} = ?", $value);
109
    }
110
111
112
113
    /**
114
     * Find and return first object found by search criteria and use
115
     * its data to populate this instance.
116
     *
117
     * The search criteria `$where` of can be set up like this:
118
     *  `id = ?`
119
     *  `id1 = ? and id2 = ?`
120
     *
121
     * The `$value` can be a single value or an array of values.
122
     *
123
     * @param string $where to use in where statement.
124
     * @param mixed  $value to use in where statement.
125
     *
126
     * @return this
127
     */
128 3 View Code Duplication
    public function findWhere($where, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 3
        $this->checkDb();
131 3
        $params = is_array($value) ? $value : [$value];
132 3
        return $this->db->connect()
133 3
                        ->select()
134 3
                        ->from($this->tableName)
135 3
                        ->where($where)
136 3
                        ->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...
137 3
                        ->fetchInto($this);
138
    }
139
140
141
142
    /**
143
     * Find and return all.
144
     *
145
     * @return array of object of this class
146
     */
147 1
    public function findAll()
148
    {
149 1
        $this->checkDb();
150 1
        return $this->db->connect()
151 1
                        ->select()
152 1
                        ->from($this->tableName)
153 1
                        ->execute()
154 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...
155
    }
156
157
158
159
    /**
160
     * Find and return all matching the search criteria.
161
     *
162
     * The search criteria `$where` of can be set up like this:
163
     *  `id = ?`
164
     *  `id IN [?, ?]`
165
     *
166
     * The `$value` can be a single value or an array of values.
167
     *
168
     * @param string $where to use in where statement.
169
     * @param mixed  $value to use in where statement.
170
     *
171
     * @return array of object of this class
172
     */
173 1 View Code Duplication
    public function findAllWhere($where, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175 1
        $this->checkDb();
176 1
        $params = is_array($value) ? $value : [$value];
177 1
        return $this->db->connect()
178 1
                        ->select()
179 1
                        ->from($this->tableName)
180 1
                        ->where($where)
181 1
                        ->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...
182 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...
183
    }
184
185
186
187
    /**
188
     * Save current object/row, insert if id is missing and do an
189
     * update if the id exists.
190
     *
191
     * @return void
192
     */
193 6
    public function save()
194
    {
195 6
        if (isset($this->{$this->tableIdColumn})) {
196 2
            return $this->update();
197
        }
198
199 6
        return $this->create();
200
    }
201
202
203
204
    /**
205
     * Create new row.
206
     *
207
     * @return void
208
     */
209 6 View Code Duplication
    protected function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
    {
211 6
        $this->checkDb();
212 5
        $properties = $this->getProperties();
213 5
        unset($properties[$this->tableIdColumn]);
214 5
        $columns = array_keys($properties);
215 5
        $values  = array_values($properties);
216
217 5
        $this->db->connect()
218 5
                 ->insert($this->tableName, $columns)
219 5
                 ->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...
220
221 5
        $this->{$this->tableIdColumn} = $this->db->lastInsertId();
222 5
    }
223
224
225
226
    /**
227
     * Update row.
228
     *
229
     * @return void
230
     */
231 2 View Code Duplication
    protected function update()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233 2
        $this->checkDb();
234 2
        $properties = $this->getProperties();
235 2
        unset($properties[$this->tableIdColumn]);
236 2
        $columns = array_keys($properties);
237 2
        $values  = array_values($properties);
238 2
        $values[] = $this->{$this->tableIdColumn};
239
240 2
        $this->db->connect()
241 2
                 ->update($this->tableName, $columns)
242 2
                 ->where("{$this->tableIdColumn} = ?")
243 2
                 ->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...
244 2
    }
245
246
247
248
    /**
249
     * Delete row.
250
     *
251
     * @param integer $id to delete or use $this->{$this->tableIdColumn} as default.
252
     *
253
     * @return void
254
     */
255 1
    public function delete($id = null)
256
    {
257 1
        $this->checkDb();
258 1
        $id = $id ?: $this->{$this->tableIdColumn};
259
260 1
        $this->db->connect()
261 1
                 ->deleteFrom($this->tableName)
262 1
                 ->where("{$this->tableIdColumn} = ?")
263 1
                 ->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...
264
265 1
        $this->{$this->tableIdColumn} = null;
266 1
    }
267
}
268