Completed
Push — master ( 5fb12e...90065f )
by Adeniyi
04:19 queued 02:11
created

Model::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
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\UserNotFoundException;
19
use Ibonly\PotatoORM\EmptyDatabaseException;
20
use Ibonly\PotatoORM\SaveUserExistException;
21
use Ibonly\PotatoORM\ColumnNotExistExeption;
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
        return end($nameOfClass);
39
    }
40
41
    /**
42
     * getClassName()
43
     *
44
     * @return string
45
     */
46
    public static function getClassName()
47
    {
48
        return self::pluralize(self::stripclassName());
49
    }
50
51
    /**
52
     * getTableName()
53
     *
54
     * @return string
55
     */
56
    public static function getTableName($connection)
57
    {
58
        return DatabaseQuery::checkTableName(self::getClassName(), $connection);
59
    }
60
61
    /**
62
     * getALL()
63
     * Get all record from the database
64
     *
65
     * @return object
66
     */
67
    public function getALL($dbConnection = NULL)
68
    {
69
        $connection = DatabaseQuery::checkConnection($dbConnection);
70
71
        $sqlQuery = DatabaseQuery::selectAllQuery(self::getTableName($connection));
72
        $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...
73
        $query->execute();
74
        if ( $query->rowCount() )
75
        {
76
            return new GetData($query->fetchAll($connection::FETCH_ASSOC));
77
        }
78
        throw new EmptyDatabaseException();
79
    }
80
81
    /**
82
     * where($data, $condition)
83
     * Get data from database where $data = $condition
84
     *
85
     * @return object
86
     */
87
    public function where($data, $condition = NULL, $dbConnection = NULL)
88
    {
89
        $databaseQuery = new DatabaseQuery();
90
        $connection = $databaseQuery->checkConnection($dbConnection);
91
92
        $sqlQuery = $databaseQuery->selectQuery(self::getTableName($connection), $data, $condition, $connection);
93
        $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...
94
        $query->execute();
95
        if ( $query->rowCount() )
96
        {
97
            return new GetData($query->fetchAll($connection::FETCH_ASSOC));
98
        }
99
        throw new UserNotFoundException();
100
    }
101
102
    /**
103
     * find($value)
104
     * Find data from database where id = $value
105
     *
106
     * @return array
107
     */
108
    public static function find($value, $dbConnection = NULL)
109
    {
110
        $connection = DatabaseQuery::checkConnection($dbConnection);
111
112
        $sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), ['id' => $value], NULL, $connection);
113
        $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...
114
        $query->execute();
115
        if ( $query->rowCount() )
116
        {
117
            $found = new static;
118
            $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...
119
            $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...
120
            return $found;
121
        }
122
        throw new UserNotFoundException();
123
    }
124
125
    /**
126
     * save()
127
     * Insert data into database
128
     *
129
     * @return bool
130
     */
131
    public function save($dbConnection = NULL)
132
    {
133
        $connection = DatabaseQuery::checkConnection($dbConnection);
134
135
        $query = $this->insertQuery(self::getTableName($connection));
136
        // $statement = $connection->prepare($query);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
        // if( $statement->execute() )
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
        // {
139
        //     return true;
140
        // }
141
        // throw new  SaveUserExistException();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
142
        return $query;
143
144
    }
145
146
    /**
147
     * update()
148
     * Update details in database after ::find(2)
149
     *
150
     * @return bool
151
     */
152
    public function update($dbConnection = NULL)
153
    {
154
        $connection = DatabaseQuery::checkConnection($dbConnection);
155
156
        $updateQuery = $this->updateQuery(self::getTableName($connection));
157
        $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...
158
        if( $statement->execute() )
159
        {
160
            return true;
161
        }
162
        throw new  SaveUserExistException();
163
    }
164
165
    /**
166
     * destroy($value)
167
     * Delete data from database
168
     *
169
     * @return bool
170
     */
171
    public function destroy($value, $dbConnection = NULL)
172
    {
173
        $connection = DatabaseQuery::checkConnection($dbConnection);
174
175
        $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...
176
        $query->execute();
177
        $check = $query->rowCount();
178
        if ($check)
179
        {
180
            return true;
181
        }
182
        throw new UserNotFoundException;
183
    }
184
}