Completed
Push — test ( bfd428...f8f0c5 )
by Temitope
02:32
created
src/Database/DatabaseHandler.php 1 patch
Indentation   +154 added lines, -154 removed lines patch added patch discarded remove patch
@@ -18,133 +18,133 @@  discard block
 block discarded – undo
18 18
 
19 19
 class DatabaseHandler {
20 20
 
21
-    private $tableFields;
22
-    private $dbHelperInstance;
23
-    private $dbConnection;
24
-    private $model;
21
+	private $tableFields;
22
+	private $dbHelperInstance;
23
+	private $dbConnection;
24
+	private $model;
25 25
     
26
-    /**
27
-     * This is a constructor; a default method  that will be called automatically during class instantiation
28
-     */
29
-    public function __construct($modelClassName, $dbConn = Null) 
30
-    {
31
-        if (is_null($dbConn)) {
32
-            $this->dbConnection = new DatabaseConnection();
33
-
34
-        } else {
35
-            $this->dbConnection = $dbConn;
36
-
37
-        }
26
+	/**
27
+	 * This is a constructor; a default method  that will be called automatically during class instantiation
28
+	 */
29
+	public function __construct($modelClassName, $dbConn = Null) 
30
+	{
31
+		if (is_null($dbConn)) {
32
+			$this->dbConnection = new DatabaseConnection();
33
+
34
+		} else {
35
+			$this->dbConnection = $dbConn;
36
+
37
+		}
38 38
         
39
-        $this->model = $modelClassName;
40
-     }
39
+		$this->model = $modelClassName;
40
+	 }
41 41
      
42
-    /**
43
-     * This method create a record and store it in a table row
44
-     * @params associative array, string tablename
45
-     * @return boolean true or false
46
-     */
47
-    public function create($associative1DArray, $tableName, $dbConn = Null) 
48
-    {
49
-        $tableFields = $this->getColumnNames($this->model, $this->dbConnection);
50
-
51
-        $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields, $associative1DArray);
42
+	/**
43
+	 * This method create a record and store it in a table row
44
+	 * @params associative array, string tablename
45
+	 * @return boolean true or false
46
+	 */
47
+	public function create($associative1DArray, $tableName, $dbConn = Null) 
48
+	{
49
+		$tableFields = $this->getColumnNames($this->model, $this->dbConnection);
50
+
51
+		$unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($tableFields, $associative1DArray);
52 52
       
53
-        if (count($unexpectedFields) > 0) {
54
-            throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as a table field");
55
-        }
53
+		if (count($unexpectedFields) > 0) {
54
+			throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields,"needs to be created as a table field");
55
+		}
56 56
 
57
-        unset($associative1DArray[0]);
57
+		unset($associative1DArray[0]);
58 58
 
59
-        if (is_null($dbConn)) {
60
-            $dbConn = $this->dbConnection;
59
+		if (is_null($dbConn)) {
60
+			$dbConn = $this->dbConnection;
61 61
 
62
-        }
62
+		}
63 63
 
64
-        return $this->insertRecord($dbConn, $tableName, $associative1DArray);
64
+		return $this->insertRecord($dbConn, $tableName, $associative1DArray);
65 65
 
66
-    }
66
+	}
67 67
     
68
-    /**
69
-     * This method runs the insertion query
70
-     * @param  $dbConn           
71
-     * @param  $tableName          
72
-     * @param  $associative1DArray 
73
-     * @return boolean true         
74
-     */
75
-    private function  insertRecord($dbConn, $tableName, $associative1DArray) 
76
-    {
77
-        $insertQuery = 'INSERT INTO '.$tableName;
68
+	/**
69
+	 * This method runs the insertion query
70
+	 * @param  $dbConn           
71
+	 * @param  $tableName          
72
+	 * @param  $associative1DArray 
73
+	 * @return boolean true         
74
+	 */
75
+	private function  insertRecord($dbConn, $tableName, $associative1DArray) 
76
+	{
77
+		$insertQuery = 'INSERT INTO '.$tableName;
78 78
 
79
-        $TableValues = implode(',',array_keys($associative1DArray));
79
+		$TableValues = implode(',',array_keys($associative1DArray));
80 80
 
81
-        foreach ($associative1DArray as $field => $value) {
82
-            $FormValues[] = "'".trim(addslashes($value))."'";
83
-        }
81
+		foreach ($associative1DArray as $field => $value) {
82
+			$FormValues[] = "'".trim(addslashes($value))."'";
83
+		}
84 84
 
85
-        $splittedTableValues = implode(',', $FormValues);
85
+		$splittedTableValues = implode(',', $FormValues);
86 86
 
87
-        $insertQuery.= ' ('.$TableValues.')';
88
-        $insertQuery.= ' VALUES ('.$splittedTableValues.')';
87
+		$insertQuery.= ' ('.$TableValues.')';
88
+		$insertQuery.= ' VALUES ('.$splittedTableValues.')';
89 89
 
90
-        $executeQuery = $dbConn->exec($insertQuery);
90
+		$executeQuery = $dbConn->exec($insertQuery);
91 91
 
92
-        if ($executeQuery) {
93
-            return true;
94
-        }
92
+		if ($executeQuery) {
93
+			return true;
94
+		}
95 95
 
96
-        return false;
96
+		return false;
97 97
 
98
-        throw NoRecordInsertionException::checkNoRecordAddedException("Record not inserted successfully");
99
-      }
98
+		throw NoRecordInsertionException::checkNoRecordAddedException("Record not inserted successfully");
99
+	  }
100 100
 
101
-    /**
102
-     * This method updates any table by supplying 3 parameter
103
-     * @params: $updateParams, $tableName, $associative1DArray
104
-     * @return boolean true or false
105
-     */
106
-    public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null)
107
-    {
108
-        $sql = "";
101
+	/**
102
+	 * This method updates any table by supplying 3 parameter
103
+	 * @params: $updateParams, $tableName, $associative1DArray
104
+	 * @return boolean true or false
105
+	 */
106
+	public function update(array $updateParams, $tableName, $associative1DArray, $dbConn = null)
107
+	{
108
+		$sql = "";
109 109
 
110
-        if (is_null($dbConn)) {
111
-            $dbConn = $this->dbConnection;
110
+		if (is_null($dbConn)) {
111
+			$dbConn = $this->dbConnection;
112 112
 
113
-        }
113
+		}
114 114
 
115
-        $updateSql = "UPDATE `$tableName` SET ";
115
+		$updateSql = "UPDATE `$tableName` SET ";
116 116
 
117
-        unset($associative1DArray['id']);
117
+		unset($associative1DArray['id']);
118 118
 
119
-        $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray);
119
+		$unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray);
120 120
 
121
-        if (count($unexpectedFields) > 0) {
122
-            throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field");
123
-        }
121
+		if (count($unexpectedFields) > 0) {
122
+			throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field");
123
+		}
124 124
 
125
-        foreach ($associative1DArray as $field => $value) {
126
-            $sql .= "`$field` = '$value'".",";
127
-        }
125
+		foreach ($associative1DArray as $field => $value) {
126
+			$sql .= "`$field` = '$value'".",";
127
+		}
128 128
 
129
-        $updateSql .= $this->prepareUpdateQuery($sql);
129
+		$updateSql .= $this->prepareUpdateQuery($sql);
130 130
 
131
-        foreach ($updateParams as $key => $val) {
132
-            $updateSql .= " WHERE $key = $val";
133
-        }
131
+		foreach ($updateParams as $key => $val) {
132
+			$updateSql .= " WHERE $key = $val";
133
+		}
134 134
 
135
-        $stmt = $dbConn->prepare($updateSql);
135
+		$stmt = $dbConn->prepare($updateSql);
136 136
 
137
-        $boolResponse = $stmt->execute();
137
+		$boolResponse = $stmt->execute();
138 138
 
139
-        if ($boolResponse) {
140
-            return true;
139
+		if ($boolResponse) {
140
+			return true;
141 141
 
142
-        }
142
+		}
143 143
 
144
-        return false;
144
+		return false;
145 145
 
146
-        throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully");
147
-    }
146
+		throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully");
147
+	}
148 148
     
149 149
   /**
150 150
    * This method retrieves record from a table
@@ -153,26 +153,26 @@  discard block
 block discarded – undo
153 153
    */
154 154
    public static function read($id, $tableName, $dbConn = null)
155 155
    {
156
-       $tableData = [];
156
+	   $tableData = [];
157 157
 
158
-       if (is_null($dbConn)) {
159
-           $dbConn = new DatabaseConnection();
160
-       }
158
+	   if (is_null($dbConn)) {
159
+		   $dbConn = new DatabaseConnection();
160
+	   }
161 161
 
162
-       $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
162
+	   $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
163 163
 
164
-       $stmt = $dbConn->prepare($sql);
165
-       $stmt->bindValue(':table', $tableName);
166
-       $stmt->bindValue(':id', $id);
167
-       $stmt->execute();
164
+	   $stmt = $dbConn->prepare($sql);
165
+	   $stmt->bindValue(':table', $tableName);
166
+	   $stmt->bindValue(':id', $id);
167
+	   $stmt->execute();
168 168
 
169
-       $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
169
+	   $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
170 170
 
171
-       foreach ($results as $result) {
172
-           array_push($tableData, $result);
173
-       }
171
+	   foreach ($results as $result) {
172
+		   array_push($tableData, $result);
173
+	   }
174 174
 
175
-       return $tableData;
175
+	   return $tableData;
176 176
 
177 177
    }
178 178
    
@@ -183,20 +183,20 @@  discard block
 block discarded – undo
183 183
    */
184 184
   public static function delete($id, $tableName, $dbConn = null)
185 185
   {
186
-      if (is_null($dbConn)) {
187
-          $dbConn = new DatabaseConnection();
188
-      }
186
+	  if (is_null($dbConn)) {
187
+		  $dbConn = new DatabaseConnection();
188
+	  }
189 189
 
190
-      $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
190
+	  $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
191 191
 
192
-      $boolResponse = $dbConn->exec($sql);
192
+	  $boolResponse = $dbConn->exec($sql);
193 193
 
194
-      if ($boolResponse) {
195
-        return true;
194
+	  if ($boolResponse) {
195
+		return true;
196 196
 
197
-      }
197
+	  }
198 198
 
199
-      throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully");
199
+	  throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully");
200 200
   }
201 201
   
202 202
   /**
@@ -207,15 +207,15 @@  discard block
 block discarded – undo
207 207
    */
208 208
   public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray)
209 209
   {
210
-      $unexpectedFields = [];
210
+	  $unexpectedFields = [];
211 211
 
212
-      foreach ($userSetterArray as $key => $val) {
213
-          if (!in_array($key,$tableColumn)) {
214
-              $unexpectedFields[] = $key;
215
-          }
216
-      }
212
+	  foreach ($userSetterArray as $key => $val) {
213
+		  if (!in_array($key,$tableColumn)) {
214
+			  $unexpectedFields[] = $key;
215
+		  }
216
+	  }
217 217
 
218
-      return $unexpectedFields;
218
+	  return $unexpectedFields;
219 219
 
220 220
   }
221 221
   
@@ -226,13 +226,13 @@  discard block
 block discarded – undo
226 226
    */
227 227
   public function prepareUpdateQuery($sql)
228 228
   {
229
-      $splittedQuery = explode(",",$sql);
229
+	  $splittedQuery = explode(",",$sql);
230 230
 
231
-      array_pop($splittedQuery);
231
+	  array_pop($splittedQuery);
232 232
 
233
-      $mergeData = implode(",",$splittedQuery);
233
+	  $mergeData = implode(",",$splittedQuery);
234 234
 
235
-      return $mergeData;
235
+	  return $mergeData;
236 236
 
237 237
   }
238 238
   
@@ -245,27 +245,27 @@  discard block
 block discarded – undo
245 245
    */
246 246
   public function findAndWhere($params, $tableName, $dbConn = null)
247 247
   {
248
-      if (is_null($dbConn)) {
249
-          $dbConn = $this->dbConnection;
250
-      }
248
+	  if (is_null($dbConn)) {
249
+		  $dbConn = $this->dbConnection;
250
+	  }
251 251
 
252
-      if (is_array($params) && !empty($params)) {
253
-          $sql = "SELECT * FROM ".$tableName;
252
+	  if (is_array($params) && !empty($params)) {
253
+		  $sql = "SELECT * FROM ".$tableName;
254 254
 
255
-          foreach ($params as $key => $val) {
256
-              $sql .= " WHERE `$key` = '$val'";
257
-          }
255
+		  foreach ($params as $key => $val) {
256
+			  $sql .= " WHERE `$key` = '$val'";
257
+		  }
258 258
 
259
-          $statement = $dbConn->prepare($sql);
260
-          $statement->execute();
259
+		  $statement = $dbConn->prepare($sql);
260
+		  $statement->execute();
261 261
 
262
-          $returnedRowNumbers = $statement->rowCount();
262
+		  $returnedRowNumbers = $statement->rowCount();
263 263
           
264
-          return $returnedRowNumbers ? true : false;
264
+		  return $returnedRowNumbers ? true : false;
265 265
 
266
-      }
266
+	  }
267 267
 
268
-      throw EmptyArrayException::checkEmptyArrayException("Record deletion unsuccessful because id does not match any record");
268
+	  throw EmptyArrayException::checkEmptyArrayException("Record deletion unsuccessful because id does not match any record");
269 269
   }
270 270
   
271 271
   /**
@@ -276,25 +276,25 @@  discard block
 block discarded – undo
276 276
    */
277 277
  public function getColumnNames($table, $dbConn = null) 
278 278
  {
279
-     $tableFields = [];
279
+	 $tableFields = [];
280 280
      
281
-     if (is_null($dbConn)) {
282
-         $dbConn = $this->dbConnection;
283
-     }
281
+	 if (is_null($dbConn)) {
282
+		 $dbConn = $this->dbConnection;
283
+	 }
284 284
 
285
-     $sql = "SHOW COLUMNS FROM ".$table;
285
+	 $sql = "SHOW COLUMNS FROM ".$table;
286 286
      
287
-     $stmt = $dbConn->prepare($sql);
288
-     $stmt->bindValue(':table', $table, PDO::PARAM_STR);
289
-     $stmt->execute();
287
+	 $stmt = $dbConn->prepare($sql);
288
+	 $stmt->bindValue(':table', $table, PDO::PARAM_STR);
289
+	 $stmt->execute();
290 290
 
291
-     $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
291
+	 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
292 292
      
293
-     foreach ($results as $result) {
294
-         array_push($tableFields, $result['Field']);
295
-     }
293
+	 foreach ($results as $result) {
294
+		 array_push($tableFields, $result['Field']);
295
+	 }
296 296
 
297
-     return $tableFields;
297
+	 return $tableFields;
298 298
      
299 299
    }
300 300
 
Please login to merge, or discard this patch.