Completed
Push — master ( 34e8c2...287087 )
by Mikael
01:39
created

ActiveRecordModel::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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
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 4
    protected function checkDb()
49
    {
50 4
        if (!$this->db) {
51 1
            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 1
    public function find($column, $value)
83
    {
84 1
        $this->checkDb();
85 1
        return $this->db->connect()
86 1
                        ->select()
87 1
                        ->from($this->tableName)
88 1
                        ->where("$column = ?")
89 1
                        ->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 1
                        ->fetchInto($this);
91
    }
92
93
94
95
    /**
96
     * Find and return all.
97
     *
98
     * @return array of object of this class
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 1
    public function findAllWhere($where, $value)
122
    {
123 1
        $this->checkDb();
124 1
        $params = is_array($value) ? $value : [$value];
125 1
        return $this->db->connect()
126 1
                        ->select()
127 1
                        ->from($this->tableName)
128 1
                        ->where($where)
129 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...
130 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...
131
    }
132
133
134
135
    /**
136
     * Save current object/row, insert if id is missing and do an
137
     * update if the id exists.
138
     *
139
     * @return void
140
     */
141 4
    public function save()
142
    {
143 4
        if (isset($this->id)) {
144
            return $this->update();
145
        }
146
147 4
        return $this->create();
148
    }
149
150
151
152
    /**
153
     * Create new row.
154
     *
155
     * @return void
156
     */
157 4 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...
158
    {
159 4
        $this->checkDb();
160 3
        $properties = $this->getProperties();
161 3
        unset($properties['id']);
162 3
        $columns = array_keys($properties);
163 3
        $values  = array_values($properties);
164
165 3
        $this->db->connect()
166 3
                 ->insert($this->tableName, $columns)
167 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...
168
169 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...
170 3
    }
171
172
173
174
    /**
175
     * Update row.
176
     *
177
     * @return void
178
     */
179 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...
180
    {
181
        $this->checkDb();
182
        $properties = $this->getProperties();
183
        unset($properties['id']);
184
        $columns = array_keys($properties);
185
        $values  = array_values($properties);
186
        $values[] = $this->id;
187
188
        $this->db->connect()
189
                 ->update($this->tableName, $columns)
190
                 ->where("id = ?")
191
                 ->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...
192
    }
193
194
195
196
    /**
197
     * Delete row.
198
     *
199
     * @param integer $id to delete or use $this->id as default.
200
     *
201
     * @return void
202
     */
203
    public function delete($id = null)
204
    {
205
        $this->checkDb();
206
        $id = $id ?: $this->id;
207
208
        $this->db->connect()
209
                 ->deleteFrom($this->tableName)
210
                 ->where("id = ?")
211
                 ->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...
212
213
        $this->id = null;
214
    }
215
}
216