Completed
Pull Request — master (#4)
by Oyebanji Jacob
05:22 queued 02:14
created

Model::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Pyjac\ORM;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use PDO;
7
use Pyjac\ORM\Exception\ModelNotFoundException;
8
9
abstract class Model implements ModelInterface
10
{
11
    protected $properties = [];
12
13
    /**
14
     * Store instance of database connection used.
15
     *
16
     * @var Pyjac\ORM\DatabaseConnection
17
     */
18
    protected $databaseConnection;
19
20
     /**
21
      *  The id of the model.
22
      *
23
      * @property string $id
24
      */
25
26
     /**
27
      * Create a model instance.
28
      */
29
     public function __construct(DatabaseConnectionInterface $databaseConnection = null)
30
     {
31
         if ($databaseConnection == null) {
32
             $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection;
33
         }
34
         $this->databaseConnection = $databaseConnection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $databaseConnection can also be of type object<Pyjac\ORM\DatabaseConnectionInterface>. However, the property $databaseConnection is declared as type object<Pyjac\ORM\Pyjac\ORM\DatabaseConnection>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35
     }
36
37
    /**
38
     * Sets into $properties the $key => $value pairs.
39
     *
40
     * @param string $key
41
     * @param string $val
42
     */
43
    public function __set($key, $val)
44
    {
45
        $this->properties[$key] = $val;
46
    }
47
48
    /**
49
     * @param string $key
50
     *
51
     * @return array
52
     */
53
    public function __get($key)
54
    {
55
        return $this->properties[$key];
56
    }
57
58
     /**
59
      * Get all the model properties.
60
      *
61
      * @return array
62
      */
63
     public function getProperties()
64
     {
65
         return $this->properties;
66
     }
67
68
     /**
69
      * Set model properties.
70
      */
71
     public function setProperties(array $properties)
72
     {
73
         $this->properties = $properties;
74
     }
75
76
    /**
77
     * Pluralize the name of the child class.
78
     *
79
     * @return string
80
     */
81
    public function getTableName()
82
    {
83
        $className = explode('\\', get_called_class());
84
85
        return Inflector::pluralize(strtolower(end($className)));
86
    }
87
88
    /**
89
     * Find the particular model with the passed id.
90
     *
91
     * @param int $id
92
     *
93
     * @return object
94
     */
95
    public static function find($id)
96
    {
97
        $model = new static();
98
99
        return $model->get($id);
100
    }
101
102
    /**
103
     * Get the particular model with the passed id.
104
     *
105
     * @param int $id
106
     *
107
     * @return object
108
     */
109
    public function get($id)
110
    {
111
        $sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}";
112
        $sqlStatement = $this->databaseConnection->prepare($sql);
113
        $sqlStatement->setFetchMode(PDO::FETCH_CLASS, get_called_class());
114
        $sqlStatement->execute();
115
        if ($sqlStatement->rowCount() < 1) {
116
            throw new ModelNotFoundException($id);
117
        }
118
119
        return $sqlStatement->fetch();
120
    }
121
122
    /**
123
     * Get all the models from the database.
124
     *
125
     * @return array
126
     */
127
    public static function getAll()
128
    {
129
        $model = new static();
130
131
        return $model->all();
132
    }
133
134
    /**
135
     * Returns all the models from the database.
136
     *
137
     * @return array
138
     */
139
    public function all()
140
    {
141
        $sql = "SELECT * FROM {$this->getTableName()}";
142
        $sqlStatement = $this->databaseConnection->prepare($sql);
143
        $sqlStatement->execute();
144
145
        return $sqlStatement->fetchAll(PDO::FETCH_CLASS);
146
    }
147
148
    /**
149
     * Update the model in the database.
150
     *
151
     * @return int
152
     */
153
    public function update()
154
    {
155
        $bindNameParameters = [];
156
        $sqlUpdate = 'UPDATE '.$this->getTableName().' SET ';
157 View Code Duplication
        foreach ($this->properties as $columnName => $columnValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            if ($columnName == 'id') {
159
                continue;
160
            }
161
            $bindColumnName = ':'.$columnName;
162
            $sqlUpdate .= "$columnName = $bindColumnName,";
163
            $bindNameParameters[$bindColumnName] = $columnValue;
164
        }
165
        //Remove the last comma in sql command then join it to the other query part.
166
        $sqlUpdate = substr($sqlUpdate, 0, -1).' WHERE id = :id';
167
        $sqlStatement = $this->databaseConnection->prepare($sqlUpdate);
168
        $bindNameParameters[':id'] = $this->properties['id'];
169
        $sqlStatement->execute($bindNameParameters);
170
171
        return $sqlStatement->rowCount();
172
    }
173
174
    /**
175
     * Insert the model values into the database.
176
     *
177
     * @return int
178
     */
179
    public function create()
180
    {
181
        $columnNames = '';
182
        $columnValues = '';
183
        $bindNameParameters = [];
184
        $sqlCreate = 'INSERT'.' INTO '.$this->getTableName().' (';
185 View Code Duplication
        foreach ($this->properties as $columnName => $columnValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
186
            $bindColumnName = ':'.$columnName;
187
            $columnNames .= $columnName.',';
188
            $columnValues .= $bindColumnName.',';
189
            $bindNameParameters[$bindColumnName] = $columnValue;
190
        }
191
        // Remove ending comma and whitespace.
192
        $columnNames = substr($columnNames, 0, -1);
193
        $columnValues = substr($columnValues, 0, -1);
194
195
        $sqlCreate .= $columnNames.') VALUES ('.$columnValues.')';
196
        $sqlStatement = $this->databaseConnection->prepare($sqlCreate);
197
        $sqlStatement->execute($bindNameParameters);
198
199
        return $sqlStatement->rowCount();
200
    }
201
202
    /**
203
     * Save the model data to the database.
204
     *
205
     * @return bool
206
     */
207
    public function save()
208
    {
209
        return isset($this->properties['id']) ? $this->update() : $this->create();
210
    }
211
212
    /**
213
     * Delete a model from the database.
214
     *
215
     * @param int $id
216
     *
217
     * @return bool
218
     */
219
    public static function destroy($id)
220
    {
221
        $model = new static();
222
223
        return $model->delete($id);
224
    }
225
226
    /**
227
     * Delete model from the database.
228
     *
229
     * @param int $id
230
     *
231
     * @return bool
232
     */
233
    public function delete($id)
234
    {
235
        $sql = 'DELETE'.' FROM '.self::getTableName().' WHERE id = '.$id;
236
        $sqlStatment = $this->databaseConnection->prepare($sql);
237
        $sqlStatment->execute();
238
239
        return ($sqlStatment->rowCount() > 0) ? true : false;
240
    }
241
}
242