Passed
Push — master ( 1c3f46...bb3a7a )
by Roeland
11:04
created
lib/public/AppFramework/Db/QBMapper.php 2 patches
Indentation   +252 added lines, -252 removed lines patch added patch discarded remove patch
@@ -36,257 +36,257 @@
 block discarded – undo
36 36
  */
37 37
 abstract class QBMapper {
38 38
 
39
-	/** @var string */
40
-	protected $tableName;
41
-
42
-	/** @var string */
43
-	protected $entityClass;
44
-
45
-	/** @var IDBConnection */
46
-	protected $db;
47
-
48
-	/**
49
-	 * @param IDBConnection $db Instance of the Db abstraction layer
50
-	 * @param string $tableName the name of the table. set this to allow entity
51
-	 * @param string $entityClass the name of the entity that the sql should be
52
-	 * mapped to queries without using sql
53
-	 * @since 14.0.0
54
-	 */
55
-	public function __construct(IDBConnection $db, string $tableName, string $entityClass=null){
56
-		$this->db = $db;
57
-		$this->tableName = $tableName;
58
-
59
-		// if not given set the entity name to the class without the mapper part
60
-		// cache it here for later use since reflection is slow
61
-		if($entityClass === null) {
62
-			$this->entityClass = str_replace('Mapper', '', \get_class($this));
63
-		} else {
64
-			$this->entityClass = $entityClass;
65
-		}
66
-	}
67
-
68
-
69
-	/**
70
-	 * @return string the table name
71
-	 * @since 14.0.0
72
-	 */
73
-	public function getTableName(): string {
74
-		return $this->tableName;
75
-	}
76
-
77
-
78
-	/**
79
-	 * Deletes an entity from the table
80
-	 * @param Entity $entity the entity that should be deleted
81
-	 * @return Entity the deleted entity
82
-	 * @since 14.0.0
83
-	 */
84
-	public function delete(Entity $entity): Entity {
85
-		$qb = $this->db->getQueryBuilder();
86
-
87
-		$qb->delete($this->tableName)
88
-			->where(
89
-				$qb->expr()->eq('id', $qb->createNamedParameter($entity->getId()))
90
-			);
91
-		$qb->execute();
92
-		return $entity;
93
-	}
94
-
95
-
96
-	/**
97
-	 * Creates a new entry in the db from an entity
98
-	 * @param Entity $entity the entity that should be created
99
-	 * @return Entity the saved entity with the set id
100
-	 * @since 14.0.0
101
-	 * @suppress SqlInjectionChecker
102
-	 */
103
-	public function insert(Entity $entity): Entity {
104
-		// get updated fields to save, fields have to be set using a setter to
105
-		// be saved
106
-		$properties = $entity->getUpdatedFields();
107
-
108
-		$qb = $this->db->getQueryBuilder();
109
-		$qb->insert($this->tableName);
110
-
111
-		// build the fields
112
-		foreach($properties as $property => $updated) {
113
-			$column = $entity->propertyToColumn($property);
114
-			$getter = 'get' . ucfirst($property);
115
-			$value = $entity->$getter();
116
-
117
-			$qb->setValue($column, $qb->createNamedParameter($value));
118
-		}
119
-
120
-		$qb->execute();
121
-
122
-		if($entity->id === null) {
123
-			$entity->setId((int)$qb->getLastInsertId());
124
-		}
125
-
126
-		return $entity;
127
-	}
128
-
129
-	/**
130
-	 * Tries to creates a new entry in the db from an entity and
131
-	 * updates an existing entry if duplicate keys are detected
132
-	 * by the database
133
-	 *
134
-	 * @param Entity $entity the entity that should be created/updated
135
-	 * @return Entity the saved entity with the (new) id
136
-	 * @throws \InvalidArgumentException if entity has no id
137
-	 * @since 15.0.0
138
-	 * @suppress SqlInjectionChecker
139
-	 */
140
-	public function insertOrUpdate(Entity $entity): Entity {
141
-		try {
142
-			return $this->insert($entity);
143
-		} catch (UniqueConstraintViolationException $ex) {
144
-			return $this->update($entity);
145
-		}
146
-	}
147
-
148
-	/**
149
-	 * Updates an entry in the db from an entity
150
-	 * @throws \InvalidArgumentException if entity has no id
151
-	 * @param Entity $entity the entity that should be created
152
-	 * @return Entity the saved entity with the set id
153
-	 * @since 14.0.0
154
-	 * @suppress SqlInjectionChecker
155
-	 */
156
-	public function update(Entity $entity): Entity {
157
-		// if entity wasn't changed it makes no sense to run a db query
158
-		$properties = $entity->getUpdatedFields();
159
-		if(\count($properties) === 0) {
160
-			return $entity;
161
-		}
162
-
163
-		// entity needs an id
164
-		$id = $entity->getId();
165
-		if($id === null){
166
-			throw new \InvalidArgumentException(
167
-				'Entity which should be updated has no id');
168
-		}
169
-
170
-		// get updated fields to save, fields have to be set using a setter to
171
-		// be saved
172
-		// do not update the id field
173
-		unset($properties['id']);
174
-
175
-		$qb = $this->db->getQueryBuilder();
176
-		$qb->update($this->tableName);
177
-
178
-		// build the fields
179
-		foreach($properties as $property => $updated) {
180
-			$column = $entity->propertyToColumn($property);
181
-			$getter = 'get' . ucfirst($property);
182
-			$value = $entity->$getter();
183
-
184
-			$qb->set($column, $qb->createNamedParameter($value));
185
-		}
186
-
187
-		$qb->where(
188
-			$qb->expr()->eq('id', $qb->createNamedParameter($id))
189
-		);
190
-		$qb->execute();
191
-
192
-		return $entity;
193
-	}
194
-
195
-	/**
196
-	 * Returns an db result and throws exceptions when there are more or less
197
-	 * results
198
-	 *
199
-	 * @see findEntity
200
-	 *
201
-	 * @param IQueryBuilder $query
202
-	 * @throws DoesNotExistException if the item does not exist
203
-	 * @throws MultipleObjectsReturnedException if more than one item exist
204
-	 * @return array the result as row
205
-	 * @since 14.0.0
206
-	 */
207
-	protected function findOneQuery(IQueryBuilder $query): array {
208
-		$cursor = $query->execute();
209
-
210
-		$row = $cursor->fetch();
211
-		if($row === false) {
212
-			$cursor->closeCursor();
213
-			$msg = $this->buildDebugMessage(
214
-				'Did expect one result but found none when executing', $query
215
-			);
216
-			throw new DoesNotExistException($msg);
217
-		}
218
-
219
-		$row2 = $cursor->fetch();
220
-		$cursor->closeCursor();
221
-		if($row2 !== false ) {
222
-			$msg = $this->buildDebugMessage(
223
-				'Did not expect more than one result when executing', $query
224
-			);
225
-			throw new MultipleObjectsReturnedException($msg);
226
-		}
227
-
228
-		return $row;
229
-	}
230
-
231
-	/**
232
-	 * @param string $msg
233
-	 * @param IQueryBuilder $sql
234
-	 * @return string
235
-	 * @since 14.0.0
236
-	 */
237
-	private function buildDebugMessage(string $msg, IQueryBuilder $sql): string {
238
-		return $msg .
239
-			': query "' . $sql->getSQL() . '"; ';
240
-	}
241
-
242
-
243
-	/**
244
-	 * Creates an entity from a row. Automatically determines the entity class
245
-	 * from the current mapper name (MyEntityMapper -> MyEntity)
246
-	 *
247
-	 * @param array $row the row which should be converted to an entity
248
-	 * @return Entity the entity
249
-	 * @since 14.0.0
250
-	 */
251
-	protected function mapRowToEntity(array $row): Entity {
252
-		return \call_user_func($this->entityClass .'::fromRow', $row);
253
-	}
254
-
255
-
256
-	/**
257
-	 * Runs a sql query and returns an array of entities
258
-	 *
259
-	 * @param IQueryBuilder $query
260
-	 * @return Entity[] all fetched entities
261
-	 * @since 14.0.0
262
-	 */
263
-	protected function findEntities(IQueryBuilder $query): array {
264
-		$cursor = $query->execute();
265
-
266
-		$entities = [];
267
-
268
-		while($row = $cursor->fetch()){
269
-			$entities[] = $this->mapRowToEntity($row);
270
-		}
271
-
272
-		$cursor->closeCursor();
273
-
274
-		return $entities;
275
-	}
276
-
277
-
278
-	/**
279
-	 * Returns an db result and throws exceptions when there are more or less
280
-	 * results
281
-	 *
282
-	 * @param IQueryBuilder $query
283
-	 * @throws DoesNotExistException if the item does not exist
284
-	 * @throws MultipleObjectsReturnedException if more than one item exist
285
-	 * @return Entity the entity
286
-	 * @since 14.0.0
287
-	 */
288
-	protected function findEntity(IQueryBuilder $query): Entity {
289
-		return $this->mapRowToEntity($this->findOneQuery($query));
290
-	}
39
+    /** @var string */
40
+    protected $tableName;
41
+
42
+    /** @var string */
43
+    protected $entityClass;
44
+
45
+    /** @var IDBConnection */
46
+    protected $db;
47
+
48
+    /**
49
+     * @param IDBConnection $db Instance of the Db abstraction layer
50
+     * @param string $tableName the name of the table. set this to allow entity
51
+     * @param string $entityClass the name of the entity that the sql should be
52
+     * mapped to queries without using sql
53
+     * @since 14.0.0
54
+     */
55
+    public function __construct(IDBConnection $db, string $tableName, string $entityClass=null){
56
+        $this->db = $db;
57
+        $this->tableName = $tableName;
58
+
59
+        // if not given set the entity name to the class without the mapper part
60
+        // cache it here for later use since reflection is slow
61
+        if($entityClass === null) {
62
+            $this->entityClass = str_replace('Mapper', '', \get_class($this));
63
+        } else {
64
+            $this->entityClass = $entityClass;
65
+        }
66
+    }
67
+
68
+
69
+    /**
70
+     * @return string the table name
71
+     * @since 14.0.0
72
+     */
73
+    public function getTableName(): string {
74
+        return $this->tableName;
75
+    }
76
+
77
+
78
+    /**
79
+     * Deletes an entity from the table
80
+     * @param Entity $entity the entity that should be deleted
81
+     * @return Entity the deleted entity
82
+     * @since 14.0.0
83
+     */
84
+    public function delete(Entity $entity): Entity {
85
+        $qb = $this->db->getQueryBuilder();
86
+
87
+        $qb->delete($this->tableName)
88
+            ->where(
89
+                $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId()))
90
+            );
91
+        $qb->execute();
92
+        return $entity;
93
+    }
94
+
95
+
96
+    /**
97
+     * Creates a new entry in the db from an entity
98
+     * @param Entity $entity the entity that should be created
99
+     * @return Entity the saved entity with the set id
100
+     * @since 14.0.0
101
+     * @suppress SqlInjectionChecker
102
+     */
103
+    public function insert(Entity $entity): Entity {
104
+        // get updated fields to save, fields have to be set using a setter to
105
+        // be saved
106
+        $properties = $entity->getUpdatedFields();
107
+
108
+        $qb = $this->db->getQueryBuilder();
109
+        $qb->insert($this->tableName);
110
+
111
+        // build the fields
112
+        foreach($properties as $property => $updated) {
113
+            $column = $entity->propertyToColumn($property);
114
+            $getter = 'get' . ucfirst($property);
115
+            $value = $entity->$getter();
116
+
117
+            $qb->setValue($column, $qb->createNamedParameter($value));
118
+        }
119
+
120
+        $qb->execute();
121
+
122
+        if($entity->id === null) {
123
+            $entity->setId((int)$qb->getLastInsertId());
124
+        }
125
+
126
+        return $entity;
127
+    }
128
+
129
+    /**
130
+     * Tries to creates a new entry in the db from an entity and
131
+     * updates an existing entry if duplicate keys are detected
132
+     * by the database
133
+     *
134
+     * @param Entity $entity the entity that should be created/updated
135
+     * @return Entity the saved entity with the (new) id
136
+     * @throws \InvalidArgumentException if entity has no id
137
+     * @since 15.0.0
138
+     * @suppress SqlInjectionChecker
139
+     */
140
+    public function insertOrUpdate(Entity $entity): Entity {
141
+        try {
142
+            return $this->insert($entity);
143
+        } catch (UniqueConstraintViolationException $ex) {
144
+            return $this->update($entity);
145
+        }
146
+    }
147
+
148
+    /**
149
+     * Updates an entry in the db from an entity
150
+     * @throws \InvalidArgumentException if entity has no id
151
+     * @param Entity $entity the entity that should be created
152
+     * @return Entity the saved entity with the set id
153
+     * @since 14.0.0
154
+     * @suppress SqlInjectionChecker
155
+     */
156
+    public function update(Entity $entity): Entity {
157
+        // if entity wasn't changed it makes no sense to run a db query
158
+        $properties = $entity->getUpdatedFields();
159
+        if(\count($properties) === 0) {
160
+            return $entity;
161
+        }
162
+
163
+        // entity needs an id
164
+        $id = $entity->getId();
165
+        if($id === null){
166
+            throw new \InvalidArgumentException(
167
+                'Entity which should be updated has no id');
168
+        }
169
+
170
+        // get updated fields to save, fields have to be set using a setter to
171
+        // be saved
172
+        // do not update the id field
173
+        unset($properties['id']);
174
+
175
+        $qb = $this->db->getQueryBuilder();
176
+        $qb->update($this->tableName);
177
+
178
+        // build the fields
179
+        foreach($properties as $property => $updated) {
180
+            $column = $entity->propertyToColumn($property);
181
+            $getter = 'get' . ucfirst($property);
182
+            $value = $entity->$getter();
183
+
184
+            $qb->set($column, $qb->createNamedParameter($value));
185
+        }
186
+
187
+        $qb->where(
188
+            $qb->expr()->eq('id', $qb->createNamedParameter($id))
189
+        );
190
+        $qb->execute();
191
+
192
+        return $entity;
193
+    }
194
+
195
+    /**
196
+     * Returns an db result and throws exceptions when there are more or less
197
+     * results
198
+     *
199
+     * @see findEntity
200
+     *
201
+     * @param IQueryBuilder $query
202
+     * @throws DoesNotExistException if the item does not exist
203
+     * @throws MultipleObjectsReturnedException if more than one item exist
204
+     * @return array the result as row
205
+     * @since 14.0.0
206
+     */
207
+    protected function findOneQuery(IQueryBuilder $query): array {
208
+        $cursor = $query->execute();
209
+
210
+        $row = $cursor->fetch();
211
+        if($row === false) {
212
+            $cursor->closeCursor();
213
+            $msg = $this->buildDebugMessage(
214
+                'Did expect one result but found none when executing', $query
215
+            );
216
+            throw new DoesNotExistException($msg);
217
+        }
218
+
219
+        $row2 = $cursor->fetch();
220
+        $cursor->closeCursor();
221
+        if($row2 !== false ) {
222
+            $msg = $this->buildDebugMessage(
223
+                'Did not expect more than one result when executing', $query
224
+            );
225
+            throw new MultipleObjectsReturnedException($msg);
226
+        }
227
+
228
+        return $row;
229
+    }
230
+
231
+    /**
232
+     * @param string $msg
233
+     * @param IQueryBuilder $sql
234
+     * @return string
235
+     * @since 14.0.0
236
+     */
237
+    private function buildDebugMessage(string $msg, IQueryBuilder $sql): string {
238
+        return $msg .
239
+            ': query "' . $sql->getSQL() . '"; ';
240
+    }
241
+
242
+
243
+    /**
244
+     * Creates an entity from a row. Automatically determines the entity class
245
+     * from the current mapper name (MyEntityMapper -> MyEntity)
246
+     *
247
+     * @param array $row the row which should be converted to an entity
248
+     * @return Entity the entity
249
+     * @since 14.0.0
250
+     */
251
+    protected function mapRowToEntity(array $row): Entity {
252
+        return \call_user_func($this->entityClass .'::fromRow', $row);
253
+    }
254
+
255
+
256
+    /**
257
+     * Runs a sql query and returns an array of entities
258
+     *
259
+     * @param IQueryBuilder $query
260
+     * @return Entity[] all fetched entities
261
+     * @since 14.0.0
262
+     */
263
+    protected function findEntities(IQueryBuilder $query): array {
264
+        $cursor = $query->execute();
265
+
266
+        $entities = [];
267
+
268
+        while($row = $cursor->fetch()){
269
+            $entities[] = $this->mapRowToEntity($row);
270
+        }
271
+
272
+        $cursor->closeCursor();
273
+
274
+        return $entities;
275
+    }
276
+
277
+
278
+    /**
279
+     * Returns an db result and throws exceptions when there are more or less
280
+     * results
281
+     *
282
+     * @param IQueryBuilder $query
283
+     * @throws DoesNotExistException if the item does not exist
284
+     * @throws MultipleObjectsReturnedException if more than one item exist
285
+     * @return Entity the entity
286
+     * @since 14.0.0
287
+     */
288
+    protected function findEntity(IQueryBuilder $query): Entity {
289
+        return $this->mapRowToEntity($this->findOneQuery($query));
290
+    }
291 291
 
292 292
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -52,13 +52,13 @@  discard block
 block discarded – undo
52 52
 	 * mapped to queries without using sql
53 53
 	 * @since 14.0.0
54 54
 	 */
55
-	public function __construct(IDBConnection $db, string $tableName, string $entityClass=null){
55
+	public function __construct(IDBConnection $db, string $tableName, string $entityClass = null) {
56 56
 		$this->db = $db;
57 57
 		$this->tableName = $tableName;
58 58
 
59 59
 		// if not given set the entity name to the class without the mapper part
60 60
 		// cache it here for later use since reflection is slow
61
-		if($entityClass === null) {
61
+		if ($entityClass === null) {
62 62
 			$this->entityClass = str_replace('Mapper', '', \get_class($this));
63 63
 		} else {
64 64
 			$this->entityClass = $entityClass;
@@ -109,9 +109,9 @@  discard block
 block discarded – undo
109 109
 		$qb->insert($this->tableName);
110 110
 
111 111
 		// build the fields
112
-		foreach($properties as $property => $updated) {
112
+		foreach ($properties as $property => $updated) {
113 113
 			$column = $entity->propertyToColumn($property);
114
-			$getter = 'get' . ucfirst($property);
114
+			$getter = 'get'.ucfirst($property);
115 115
 			$value = $entity->$getter();
116 116
 
117 117
 			$qb->setValue($column, $qb->createNamedParameter($value));
@@ -119,8 +119,8 @@  discard block
 block discarded – undo
119 119
 
120 120
 		$qb->execute();
121 121
 
122
-		if($entity->id === null) {
123
-			$entity->setId((int)$qb->getLastInsertId());
122
+		if ($entity->id === null) {
123
+			$entity->setId((int) $qb->getLastInsertId());
124 124
 		}
125 125
 
126 126
 		return $entity;
@@ -156,13 +156,13 @@  discard block
 block discarded – undo
156 156
 	public function update(Entity $entity): Entity {
157 157
 		// if entity wasn't changed it makes no sense to run a db query
158 158
 		$properties = $entity->getUpdatedFields();
159
-		if(\count($properties) === 0) {
159
+		if (\count($properties) === 0) {
160 160
 			return $entity;
161 161
 		}
162 162
 
163 163
 		// entity needs an id
164 164
 		$id = $entity->getId();
165
-		if($id === null){
165
+		if ($id === null) {
166 166
 			throw new \InvalidArgumentException(
167 167
 				'Entity which should be updated has no id');
168 168
 		}
@@ -176,9 +176,9 @@  discard block
 block discarded – undo
176 176
 		$qb->update($this->tableName);
177 177
 
178 178
 		// build the fields
179
-		foreach($properties as $property => $updated) {
179
+		foreach ($properties as $property => $updated) {
180 180
 			$column = $entity->propertyToColumn($property);
181
-			$getter = 'get' . ucfirst($property);
181
+			$getter = 'get'.ucfirst($property);
182 182
 			$value = $entity->$getter();
183 183
 
184 184
 			$qb->set($column, $qb->createNamedParameter($value));
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
 		$cursor = $query->execute();
209 209
 
210 210
 		$row = $cursor->fetch();
211
-		if($row === false) {
211
+		if ($row === false) {
212 212
 			$cursor->closeCursor();
213 213
 			$msg = $this->buildDebugMessage(
214 214
 				'Did expect one result but found none when executing', $query
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
 
219 219
 		$row2 = $cursor->fetch();
220 220
 		$cursor->closeCursor();
221
-		if($row2 !== false ) {
221
+		if ($row2 !== false) {
222 222
 			$msg = $this->buildDebugMessage(
223 223
 				'Did not expect more than one result when executing', $query
224 224
 			);
@@ -235,8 +235,8 @@  discard block
 block discarded – undo
235 235
 	 * @since 14.0.0
236 236
 	 */
237 237
 	private function buildDebugMessage(string $msg, IQueryBuilder $sql): string {
238
-		return $msg .
239
-			': query "' . $sql->getSQL() . '"; ';
238
+		return $msg.
239
+			': query "'.$sql->getSQL().'"; ';
240 240
 	}
241 241
 
242 242
 
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 	 * @since 14.0.0
250 250
 	 */
251 251
 	protected function mapRowToEntity(array $row): Entity {
252
-		return \call_user_func($this->entityClass .'::fromRow', $row);
252
+		return \call_user_func($this->entityClass.'::fromRow', $row);
253 253
 	}
254 254
 
255 255
 
@@ -265,7 +265,7 @@  discard block
 block discarded – undo
265 265
 
266 266
 		$entities = [];
267 267
 
268
-		while($row = $cursor->fetch()){
268
+		while ($row = $cursor->fetch()) {
269 269
 			$entities[] = $this->mapRowToEntity($row);
270 270
 		}
271 271
 
Please login to merge, or discard this patch.