Completed
Push — test ( 17f928...4c3f27 )
by Temitope
02:39
created
src/Database/DatabaseHandler.php 1 patch
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -18,131 +18,131 @@  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;
112
-        }
110
+		if (is_null($dbConn)) {
111
+			$dbConn = $this->dbConnection;
112
+		}
113 113
 
114
-        $updateSql = "UPDATE `$tableName` SET ";
114
+		$updateSql = "UPDATE `$tableName` SET ";
115 115
 
116
-        unset($associative1DArray['id']);
116
+		unset($associative1DArray['id']);
117 117
 
118
-        $unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray);
118
+		$unexpectedFields = self::checkIfMagicSetterContainsIsSameAsClassModel($this->getColumnNames($this->model, $this->dbConnection), $associative1DArray);
119 119
 
120
-        if (count($unexpectedFields) > 0) {
121
-            throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field");
122
-        }
120
+		if (count($unexpectedFields) > 0) {
121
+			throw TableFieldUndefinedException::reportUnknownTableField($unexpectedFields, "needs to be created as a table field");
122
+		}
123 123
 
124
-        foreach ($associative1DArray as $field => $value) {
125
-            $sql .= "`$field` = '$value'".",";
126
-        }
124
+		foreach ($associative1DArray as $field => $value) {
125
+			$sql .= "`$field` = '$value'".",";
126
+		}
127 127
 
128
-        $updateSql .= $this->prepareUpdateQuery($sql);
128
+		$updateSql .= $this->prepareUpdateQuery($sql);
129 129
 
130
-        foreach ($updateParams as $key => $val) {
131
-            $updateSql .= " WHERE $key = $val";
132
-        }
130
+		foreach ($updateParams as $key => $val) {
131
+			$updateSql .= " WHERE $key = $val";
132
+		}
133 133
 
134
-        $stmt = $dbConn->prepare($updateSql);
134
+		$stmt = $dbConn->prepare($updateSql);
135 135
 
136
-        $boolResponse = $stmt->execute();
136
+		$boolResponse = $stmt->execute();
137 137
 
138
-        if ($boolResponse) {
139
-            return true;
140
-        }
138
+		if ($boolResponse) {
139
+			return true;
140
+		}
141 141
 
142
-        return false;
142
+		return false;
143 143
 
144
-        throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully");
145
-    }
144
+		throw NoRecordUpdateException::checkNoRecordUpdateException("Record not updated successfully");
145
+	}
146 146
     
147 147
   /**
148 148
    * This method retrieves record from a table
@@ -151,32 +151,32 @@  discard block
 block discarded – undo
151 151
    */
152 152
    public static function read($id, $tableName, $dbConn = null)
153 153
    {
154
-       $tableData = [];
154
+	   $tableData = [];
155 155
 
156
-       if (is_null($dbConn)) {
157
-           $dbConn = new DatabaseConnection();
158
-       }
156
+	   if (is_null($dbConn)) {
157
+		   $dbConn = new DatabaseConnection();
158
+	   }
159 159
 
160
-       $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
160
+	   $sql = $id ? 'SELECT * FROM '.$tableName.' WHERE id = '.$id : 'SELECT * FROM '.$tableName;
161 161
 
162
-       try {
163
-           $stmt = $dbConn->prepare($sql);
164
-           $stmt->bindValue(':table', $tableName);
165
-           $stmt->bindValue(':id', $id);
166
-           $stmt->execute();
162
+	   try {
163
+		   $stmt = $dbConn->prepare($sql);
164
+		   $stmt->bindValue(':table', $tableName);
165
+		   $stmt->bindValue(':id', $id);
166
+		   $stmt->execute();
167 167
 
168
-       } catch (PDOException $e) {
169
-        return  $e->getMessage();
168
+	   } catch (PDOException $e) {
169
+		return  $e->getMessage();
170 170
 
171
-       }
171
+	   }
172 172
 
173
-       $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
173
+	   $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
174 174
 
175
-       foreach ($results as $result) {
176
-           array_push($tableData, $result);
177
-       }
175
+	   foreach ($results as $result) {
176
+		   array_push($tableData, $result);
177
+	   }
178 178
 
179
-       return $tableData;
179
+	   return $tableData;
180 180
 
181 181
    }
182 182
    
@@ -187,20 +187,20 @@  discard block
 block discarded – undo
187 187
    */
188 188
   public static function delete($id, $tableName, $dbConn = null)
189 189
   {
190
-      if (is_null($dbConn)) {
191
-          $dbConn = new DatabaseConnection();
192
-      }
190
+	  if (is_null($dbConn)) {
191
+		  $dbConn = new DatabaseConnection();
192
+	  }
193 193
 
194
-      $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
194
+	  $sql = 'DELETE FROM '.$tableName.' WHERE id = '.$id;
195 195
 
196
-      $boolResponse = $dbConn->exec($sql);
196
+	  $boolResponse = $dbConn->exec($sql);
197 197
 
198
-      if ($boolResponse) {
199
-        return true;
198
+	  if ($boolResponse) {
199
+		return true;
200 200
 
201
-      }
201
+	  }
202 202
 
203
-      throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully");
203
+	  throw NoRecordDeletionException::checkNoRecordDeleteException("Record not deleted successfully");
204 204
   }
205 205
   
206 206
   /**
@@ -211,15 +211,15 @@  discard block
 block discarded – undo
211 211
    */
212 212
   public static function checkIfMagicSetterContainsIsSameAsClassModel(array $tableColumn, array $userSetterArray)
213 213
   {
214
-      $unexpectedFields = [];
214
+	  $unexpectedFields = [];
215 215
 
216
-      foreach ($userSetterArray as $key => $val) {
217
-          if (!in_array($key,$tableColumn)) {
218
-              $unexpectedFields[] = $key;
219
-          }
220
-      }
216
+	  foreach ($userSetterArray as $key => $val) {
217
+		  if (!in_array($key,$tableColumn)) {
218
+			  $unexpectedFields[] = $key;
219
+		  }
220
+	  }
221 221
 
222
-      return $unexpectedFields;
222
+	  return $unexpectedFields;
223 223
 
224 224
   }
225 225
   
@@ -230,13 +230,13 @@  discard block
 block discarded – undo
230 230
    */
231 231
   public function prepareUpdateQuery($sql)
232 232
   {
233
-      $splittedQuery = explode(",",$sql);
233
+	  $splittedQuery = explode(",",$sql);
234 234
 
235
-      array_pop($splittedQuery);
235
+	  array_pop($splittedQuery);
236 236
 
237
-      $mergeData = implode(",",$splittedQuery);
237
+	  $mergeData = implode(",",$splittedQuery);
238 238
 
239
-      return $mergeData;
239
+	  return $mergeData;
240 240
 
241 241
   }
242 242
   
@@ -249,26 +249,26 @@  discard block
 block discarded – undo
249 249
    */
250 250
   public function findAndWhere($params, $tableName, $dbConn = null)
251 251
   {
252
-      if (is_null($dbConn)) {
253
-          $dbConn = $this->dbConnection;
254
-      }
252
+	  if (is_null($dbConn)) {
253
+		  $dbConn = $this->dbConnection;
254
+	  }
255 255
 
256
-      if (is_array($params) && !empty($params)) {
257
-          $sql = "SELECT * FROM ".$tableName;
256
+	  if (is_array($params) && !empty($params)) {
257
+		  $sql = "SELECT * FROM ".$tableName;
258 258
 
259
-          foreach ($params as $key => $val) {
260
-              $sql .= " WHERE `$key` = '$val'";
261
-          }
259
+		  foreach ($params as $key => $val) {
260
+			  $sql .= " WHERE `$key` = '$val'";
261
+		  }
262 262
 
263
-          $statement = $dbConn->prepare($sql);
264
-          $statement->execute();
263
+		  $statement = $dbConn->prepare($sql);
264
+		  $statement->execute();
265 265
 
266
-          $returnedRowNumbers = $statement->rowCount();
266
+		  $returnedRowNumbers = $statement->rowCount();
267 267
           
268
-          return $returnedRowNumbers ? true : false;
269
-      }
268
+		  return $returnedRowNumbers ? true : false;
269
+	  }
270 270
 
271
-      throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array");
271
+	  throw EmptyArrayException::checkEmptyArrayException("Array Expected: parameter passed to this function is not an array");
272 272
   }
273 273
   
274 274
   /**
@@ -279,25 +279,25 @@  discard block
 block discarded – undo
279 279
    */
280 280
  public function getColumnNames($table, $dbConn = null) 
281 281
  {
282
-     $tableFields = [];
282
+	 $tableFields = [];
283 283
      
284
-     if (is_null($dbConn)) {
285
-         $dbConn = $this->dbConnection;
286
-     }
284
+	 if (is_null($dbConn)) {
285
+		 $dbConn = $this->dbConnection;
286
+	 }
287 287
 
288
-     $sql = "SHOW COLUMNS FROM ".$table;
288
+	 $sql = "SHOW COLUMNS FROM ".$table;
289 289
      
290
-     $stmt = $dbConn->prepare($sql);
291
-     $stmt->bindValue(':table', $table, PDO::PARAM_STR);
292
-     $stmt->execute();
290
+	 $stmt = $dbConn->prepare($sql);
291
+	 $stmt->bindValue(':table', $table, PDO::PARAM_STR);
292
+	 $stmt->execute();
293 293
 
294
-     $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
294
+	 $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
295 295
      
296
-     foreach ($results as $result) {
297
-         array_push($tableFields, $result['Field']);
298
-     }
296
+	 foreach ($results as $result) {
297
+		 array_push($tableFields, $result['Field']);
298
+	 }
299 299
 
300
-     return $tableFields;
300
+	 return $tableFields;
301 301
      
302 302
    }
303 303
 
Please login to merge, or discard this patch.