Completed
Push — master ( 245fbf...00e192 )
by Adeniyi
04:40 queued 02:23
created

src/Helper/Model.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
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);
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);
114
        $query->execute();
115
        if ( $query->rowCount() )
116
        {
117
            $found = new static;
118
            $found->id = $value;
119
            $found->data = $query->fetchAll($connection::FETCH_ASSOC);
120
            return $found;
121
        }
122
        throw new UserNotFoundException();
123
    }
124
125
    /**
126
     * save()
127
     * Insert data into database
128
     *
129
     * @return bool
130
     */
131 View Code Duplication
    public function save($dbConnection = NULL)
0 ignored issues
show
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...
132
    {
133
        $connection = DatabaseQuery::checkConnection($dbConnection);
134
135
        $query = $this->insertQuery(self::getTableName($connection));
136
        $statement = $connection->prepare($query);
0 ignored issues
show
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...
137
        if( $statement->execute() )
138
        {
139
            return true;
140
        }
141
        throw new  SaveUserExistException();
142
143
    }
144
145
    /**
146
     * update()
147
     * Update details in database after ::find(2)
148
     *
149
     * @return bool
150
     */
151 View Code Duplication
    public function update($dbConnection = NULL)
0 ignored issues
show
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...
152
    {
153
        $connection = DatabaseQuery::checkConnection($dbConnection);
154
155
        $updateQuery = $this->updateQuery(self::getTableName($connection));
156
        $statement = $connection->prepare($updateQuery);
157
        if( $statement->execute() )
158
        {
159
            return true;
160
        }
161
        throw new  SaveUserExistException();
162
    }
163
164
    /**
165
     * destroy($value)
166
     * Delete data from database
167
     *
168
     * @return bool
169
     */
170
    public function destroy($value, $dbConnection = NULL)
171
    {
172
        $connection = DatabaseQuery::checkConnection($dbConnection);
173
174
        $query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value);
175
        $query->execute();
176
        $check = $query->rowCount();
177
        if ($check)
178
        {
179
            return true;
180
        }
181
        throw new UserNotFoundException;
182
    }
183
}