Completed
Push — master ( 50ed3b...edbf7d )
by Adeniyi
8s
created

Model::fields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
/**
3
 * PotatoORM manages the persistence of database CRUD operations.
4
 *
5
 * @package Ibonly\PotatoORM\Model
6
 * @author  Ibraheem ADENIYI <[email protected]>
7
 * @license MIT <https://opensource.org/licenses/MIT>
8
 */
9
10
namespace Ibonly\PotatoORM;
11
12
use PDO;
13
use Exception;
14
use PDOException;
15
use Ibonly\PotatoORM\GetData;
16
use Ibonly\PotatoORM\DatabaseQuery;
17
use Ibonly\PotatoORM\ModelInterface;
18
use Ibonly\PotatoORM\DataNotFoundException;
19
use Ibonly\PotatoORM\EmptyDatabaseException;
20
use Ibonly\PotatoORM\ColumnNotExistExeption;
21
use Ibonly\PotatoORM\DataAlreadyExistException;
22
use Ibonly\PotatoORM\InvalidConnectionException;
23
24
class Model extends DatabaseQuery implements ModelInterface
25
{
26
    //Inject the inflector trait
27
    use Inflector;
28
29
    /**
30
     * stripclassName()
31
     *
32
     * @return string
33
     */
34
    public static function stripclassName()
35
    {
36
        $className = strtolower(get_called_class());
37
        $nameOfClass = explode("\\", $className);
38
39
        return end($nameOfClass);
40
    }
41
42
    /**
43
     * Get the table name if defined in the model
44
     * 
45
     * @return string
46
     */
47
    public function tableName()
48
    {
49
        if(isset($this->table)) {
50
            return $this->table;
0 ignored issues
show
Bug introduced by
The property table 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...
51
        }
52
        return null;
53
    }
54
55
    /**
56
     * Get the fields to be fillables defined in the model
57
     * 
58
     * @return string
59
     */
60
    public function fields()
61
    {
62
        if (isset($this->fillables)) {
63
            if (sizeof($this->fillables) > 0) {
64
                return implode(", ", $this->fillables);
0 ignored issues
show
Bug introduced by
The property fillables 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...
65
            }
66
            return '*';
67
        }
68
        return '*';
69
    }
70
71
    /**
72
     * getClassName()
73
     *
74
     * @return string
75
     */
76
    public function getClassName()
77
    {
78
        if ($this->tableName() === null) {
79
            return self::pluralize(self::stripclassName());
80
        } else {
81
            return $this->tableName();
82
        }
83
    }
84
85
    /**
86
     * getTableName()
87
     *
88
     * @return string
89
     */
90
    public function getTableName($connection)
91
    {
92
        return DatabaseQuery::checkTableName($this->getClassName(), $connection);
93
    }
94
95
    /**
96
     * getALL()
97
     * Get all record from the database
98
     *
99
     * @return object
100
     */
101
    public function getALL($dbConnection = NULL)
102
    {
103
        $connection = DatabaseQuery::checkConnection($dbConnection);
104
105
        $sqlQuery = DatabaseQuery::selectAllQuery(self::getTableName($connection), self::fields());
106
        $query = $connection->prepare($sqlQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
107
        $query->execute();
108
        if ( $query->rowCount() )
109
        {
110
            return new GetData($query->fetchAll($connection::FETCH_ASSOC));
111
        }
112
        throw new EmptyDatabaseException();
113
    }
114
115
    /**
116
     * where($data, $condition)
117
     * Get data from database where $data = $condition
118
     *
119
     * @return object
120
     */
121
    public function where($data, $condition = NULL, $dbConnection = NULL)
122
    {
123
        $databaseQuery = new DatabaseQuery();
124
        $connection = $databaseQuery->checkConnection($dbConnection);
125
126
        $sqlQuery = $databaseQuery->selectQuery(self::getTableName($connection), $data, $condition, $connection);
127
        $query = $connection->prepare($sqlQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
128
        $query->execute();
129
        if ( $query->rowCount() )
130
        {
131
            return new GetData($query->fetchAll($connection::FETCH_ASSOC));
132
        }
133
        throw new DataNotFoundException();
134
    }
135
136
    /**
137
     * find($value)
138
     * Find data from database where id = $value
139
     *
140
     * @return array
141
     */
142
    public static function find($value, $dbConnection = NULL)
143
    {
144
        $connection = DatabaseQuery::checkConnection($dbConnection);
145
146
        $sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), ['id' => $value], NULL, $connection);
147
        $query = $connection->prepare($sqlQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
148
        $query->execute();
149
        if ( $query->rowCount() )
150
        {
151
            $found = new static;
152
            $found->id = $value;
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...
153
            $found->data = $query->fetchAll($connection::FETCH_ASSOC);
0 ignored issues
show
Bug introduced by
The property data 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...
154
            return $found;
155
        }
156
        throw new DataNotFoundException();
157
    }
158
159
    /**
160
     * save()
161
     * Insert data into database
162
     *
163
     * @return bool
164
     */
165 View Code Duplication
    public function save($dbConnection = NULL)
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...
166
    {
167
        $connection = DatabaseQuery::checkConnection($dbConnection);
168
169
        $query = $this->insertQuery(self::getTableName($connection));
170
        $statement = $connection->prepare($query);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
171
        if( $statement->execute() )
172
        {
173
            return true;
174
        }
175
        throw new  DataAlreadyExistException();
176
177
    }
178
179
    /**
180
     * update()
181
     * Update details in database after ::find(2)
182
     *
183
     * @return bool
184
     */
185 View Code Duplication
    public function update($dbConnection = NULL)
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...
186
    {
187
        $connection = DatabaseQuery::checkConnection($dbConnection);
188
189
        $updateQuery = $this->updateQuery(self::getTableName($connection));
190
        $statement = $connection->prepare($updateQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
191
        if( $statement->execute() )
192
        {
193
            return true;
194
        }
195
        throw new  DataAlreadyExistException();
196
    }
197
198
    /**
199
     * destroy($value)
200
     * Delete data from database
201
     *
202
     * @return bool
203
     */
204
    public function destroy($value, $dbConnection = NULL)
205
    {
206
        $connection = DatabaseQuery::checkConnection($dbConnection);
207
208
        $query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
209
        $query->execute();
210
        $check = $query->rowCount();
211
        if ($check)
212
        {
213
            return true;
214
        }
215
        throw new DataNotFoundException;
216
    }
217
}