| Total Complexity | 51 |
| Total Lines | 421 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 2 |
Complex classes like BaseMapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 29 | abstract class BaseMapper extends Mapper { |
||
| 30 | const SQL_DATE_FORMAT = 'Y-m-d H:i:s.v'; |
||
| 31 | |||
| 32 | protected $nameColumn; |
||
| 33 | /** @phpstan-var class-string<EntityType> $entityClass */ |
||
| 34 | protected $entityClass; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @phpstan-param class-string<EntityType> $entityClass |
||
| 38 | */ |
||
| 39 | public function __construct(IDBConnection $db, string $tableName, string $entityClass, string $nameColumn) { |
||
| 40 | parent::__construct($db, $tableName, $entityClass); |
||
| 41 | $this->nameColumn = $nameColumn; |
||
| 42 | // eclipse the base class property to help phpstan |
||
| 43 | $this->entityClass = $entityClass; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create an empty object of the entity class bound to this mapper |
||
| 48 | * @phpstan-return EntityType |
||
| 49 | */ |
||
| 50 | public function createEntity() : Entity { |
||
| 51 | return new $this->entityClass(); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Find a single entity by id and user_id |
||
| 56 | * @throws DoesNotExistException if the entity does not exist |
||
| 57 | * @throws MultipleObjectsReturnedException if more than one entity exists |
||
| 58 | * @phpstan-return EntityType |
||
| 59 | */ |
||
| 60 | public function find(int $id, string $userId) : Entity { |
||
| 61 | $sql = $this->selectUserEntities("`{$this->getTableName()}`.`id` = ?"); |
||
| 62 | return $this->findEntity($sql, [$userId, $id]); |
||
|
|
|||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Find all entities matching the given IDs. Specifying the owning user is optional. |
||
| 67 | * @param integer[] $ids IDs of the entities to be found |
||
| 68 | * @param string|null $userId |
||
| 69 | * @return Entity[] |
||
| 70 | * @phpstan-return EntityType[] |
||
| 71 | */ |
||
| 72 | public function findById(array $ids, string $userId=null) : array { |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Find all user's entities |
||
| 88 | * @param string|null $createdMin Optional minimum `created` timestamp. |
||
| 89 | * @param string|null $createdMax Optional maximum `created` timestamp. |
||
| 90 | * @param string|null $updatedMin Optional minimum `updated` timestamp. |
||
| 91 | * @param string|null $updatedMax Optional maximum `updated` timestamp. |
||
| 92 | * @return Entity[] |
||
| 93 | * @phpstan-return EntityType[] |
||
| 94 | */ |
||
| 95 | public function findAll(string $userId, int $sortBy=SortBy::None, int $limit=null, int $offset=null, |
||
| 96 | ?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array { |
||
| 97 | $sorting = $this->formatSortingClause($sortBy); |
||
| 98 | [$condition, $params] = $this->formatTimestampConditions($createdMin, $createdMax, $updatedMin, $updatedMax); |
||
| 99 | $sql = $this->selectUserEntities($condition, $sorting); |
||
| 100 | \array_unshift($params, $userId); |
||
| 101 | return $this->findEntities($sql, $params, $limit, $offset); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Find all user's entities matching the given name |
||
| 106 | * @param string|null $createdMin Optional minimum `created` timestamp. |
||
| 107 | * @param string|null $createdMax Optional maximum `created` timestamp. |
||
| 108 | * @param string|null $updatedMin Optional minimum `updated` timestamp. |
||
| 109 | * @param string|null $updatedMax Optional maximum `updated` timestamp. |
||
| 110 | * @return Entity[] |
||
| 111 | * @phpstan-return EntityType[] |
||
| 112 | */ |
||
| 113 | public function findAllByName( |
||
| 114 | ?string $name, string $userId, bool $fuzzy=false, int $limit=null, int $offset=null, |
||
| 115 | ?string $createdMin=null, ?string $createdMax=null, ?string $updatedMin=null, ?string $updatedMax=null) : array { |
||
| 116 | |||
| 117 | $nameCol = "`{$this->getTableName()}`.`{$this->nameColumn}`"; |
||
| 118 | if ($name === null) { |
||
| 119 | $condition = "$nameCol IS NULL"; |
||
| 120 | $params = [$userId]; |
||
| 121 | } elseif ($fuzzy) { |
||
| 122 | $condition = "LOWER($nameCol) LIKE LOWER(?)"; |
||
| 123 | $params = [$userId, self::prepareFuzzySearchString($name)]; |
||
| 124 | } else { |
||
| 125 | $condition = "LOWER($nameCol) = LOWER(?)"; |
||
| 126 | $params = [$userId, $name]; |
||
| 127 | } |
||
| 128 | |||
| 129 | [$timestampConds, $timestampParams] = $this->formatTimestampConditions($createdMin, $createdMax, $updatedMin, $updatedMax); |
||
| 130 | if (!empty($timestampConds)) { |
||
| 131 | $condition .= ' AND ' . $timestampConds; |
||
| 132 | $params = \array_merge($params, $timestampParams); |
||
| 133 | } |
||
| 134 | |||
| 135 | $sql = $this->selectUserEntities($condition, "ORDER BY LOWER($nameCol)"); |
||
| 136 | |||
| 137 | return $this->findEntities($sql, $params, $limit, $offset); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Find all user's starred entities |
||
| 142 | * @return Entity[] |
||
| 143 | * @phpstan-return EntityType[] |
||
| 144 | */ |
||
| 145 | public function findAllStarred(string $userId, int $limit=null, int $offset=null) : array { |
||
| 146 | $sql = $this->selectUserEntities( |
||
| 147 | "`{$this->getTableName()}`.`starred` IS NOT NULL", |
||
| 148 | "ORDER BY LOWER(`{$this->getTableName()}`.`{$this->nameColumn}`)"); |
||
| 149 | return $this->findEntities($sql, [$userId], $limit, $offset); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Find IDs of all user's entities of this kind |
||
| 154 | * @return int[] |
||
| 155 | */ |
||
| 156 | public function findAllIds(string $userId) : array { |
||
| 157 | $sql = "SELECT `id` FROM `{$this->getTableName()}` WHERE `user_id` = ?"; |
||
| 158 | $result = $this->execute($sql, [$userId]); |
||
| 159 | |||
| 160 | return \array_map('intval', $result->fetchAll(\PDO::FETCH_COLUMN)); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Find IDs of all users owning any entities of this mapper |
||
| 165 | * @return string[] |
||
| 166 | */ |
||
| 167 | public function findAllUsers() : array { |
||
| 168 | $sql = "SELECT DISTINCT(`user_id`) FROM `{$this->getTableName()}`"; |
||
| 169 | $result = $this->execute($sql); |
||
| 170 | |||
| 171 | return $result->fetchAll(\PDO::FETCH_COLUMN); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Delete all entities with given IDs without specifying the user |
||
| 176 | * @param integer[] $ids IDs of the entities to be deleted |
||
| 177 | */ |
||
| 178 | public function deleteById(array $ids) : void { |
||
| 179 | $count = \count($ids); |
||
| 180 | if ($count === 0) { |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | $this->deleteByCond('`id` IN ' . $this->questionMarks($count), $ids); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Delete all entities matching the given SQL condition |
||
| 188 | * @param string $condition SQL 'WHERE' condition (without the keyword 'WHERE') |
||
| 189 | * @param array $params SQL parameters for the condition |
||
| 190 | */ |
||
| 191 | protected function deleteByCond(string $condition, array $params) : void { |
||
| 192 | $sql = "DELETE FROM `{$this->getTableName()}` WHERE ". $condition; |
||
| 193 | $this->execute($sql, $params); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Delete all entities of the given user |
||
| 198 | */ |
||
| 199 | public function deleteAll(string $userId) : void { |
||
| 200 | $sql = "DELETE FROM `{$this->getTableName()}` WHERE `user_id` = ?"; |
||
| 201 | $this->execute($sql, [$userId]); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Tests if entity with given ID and user ID exists in the database |
||
| 206 | */ |
||
| 207 | public function exists(int $id, string $userId) : bool { |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Count all entities of a user |
||
| 215 | */ |
||
| 216 | public function count(string $userId) : int { |
||
| 217 | $sql = "SELECT COUNT(*) AS count FROM `{$this->getTableName()}` WHERE `user_id` = ?"; |
||
| 218 | $result = $this->execute($sql, [$userId]); |
||
| 219 | $row = $result->fetch(); |
||
| 220 | return \intval($row['count']); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritDoc} |
||
| 225 | * @see \OCP\AppFramework\Db\Mapper::insert() |
||
| 226 | * @phpstan-param EntityType $entity |
||
| 227 | * @phpstan-return EntityType |
||
| 228 | */ |
||
| 229 | public function insert(\OCP\AppFramework\Db\Entity $entity) : \OCP\AppFramework\Db\Entity { |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritDoc} |
||
| 251 | * @see \OCP\AppFramework\Db\Mapper::update() |
||
| 252 | * @phpstan-param EntityType $entity |
||
| 253 | * @phpstan-return EntityType |
||
| 254 | */ |
||
| 255 | public function update(\OCP\AppFramework\Db\Entity $entity) : \OCP\AppFramework\Db\Entity { |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Insert an entity, or if an entity with the same identity already exists, |
||
| 263 | * update the existing entity. |
||
| 264 | * Note: The functions insertOrUpate and updateOrInsert get the exactly same thing done. The only difference is |
||
| 265 | * that the former is optimized for cases where the entity doens't exist and the latter for cases where it does exist. |
||
| 266 | * @return Entity The inserted or updated entity, containing also the id field |
||
| 267 | * @phpstan-param EntityType $entity |
||
| 268 | * @phpstan-return EntityType |
||
| 269 | */ |
||
| 270 | public function insertOrUpdate(Entity $entity) : Entity { |
||
| 271 | try { |
||
| 272 | return $this->insert($entity); |
||
| 273 | } catch (UniqueConstraintViolationException $ex) { |
||
| 274 | $existingEntity = $this->findUniqueEntity($entity); |
||
| 275 | $entity->setId($existingEntity->getId()); |
||
| 276 | $entity->setCreated($existingEntity->getCreated()); |
||
| 277 | return $this->update($entity); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Update an entity whose unique constraint fields match the given entity. If such entity is not found, |
||
| 283 | * a new entity is inserted. |
||
| 284 | * Note: The functions insertOrUpate and updateOrInsert get the exactly same thing done. The only difference is |
||
| 285 | * that the former is optimized for cases where the entity doens't exist and the latter for cases where it does exist. |
||
| 286 | * @return Entity The inserted or updated entity, containing also the id field |
||
| 287 | * @phpstan-param EntityType $entity |
||
| 288 | * @phpstan-return EntityType |
||
| 289 | */ |
||
| 290 | public function updateOrInsert(Entity $entity) : Entity { |
||
| 291 | try { |
||
| 292 | $existingEntity = $this->findUniqueEntity($entity); |
||
| 293 | $entity->setId($existingEntity->getId()); |
||
| 294 | return $this->update($entity); |
||
| 295 | } catch (DoesNotExistException $ex) { |
||
| 296 | try { |
||
| 297 | return $this->insert($entity); |
||
| 298 | } catch (UniqueConstraintViolationException $ex) { |
||
| 299 | // the conflicting entry didn't exist an eyeblink ago but now it does |
||
| 300 | // => this is essentially a concurrent update and it is anyway non-deterministic, which |
||
| 301 | // update happens last; cancel this update |
||
| 302 | return $this->findUniqueEntity($entity); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set the "starred" column of the given entities |
||
| 309 | * @param \DateTime|null $date |
||
| 310 | * @param integer[] $ids |
||
| 311 | * @param string $userId |
||
| 312 | * @return int number of modified entities |
||
| 313 | */ |
||
| 314 | public function setStarredDate(?\DateTime $date, array $ids, string $userId) : int { |
||
| 315 | $count = \count($ids); |
||
| 316 | if (!empty($date)) { |
||
| 317 | $date = $date->format(self::SQL_DATE_FORMAT); |
||
| 318 | } |
||
| 319 | |||
| 320 | $sql = "UPDATE `{$this->getTableName()}` SET `starred` = ? |
||
| 321 | WHERE `id` IN {$this->questionMarks($count)} AND `user_id` = ?"; |
||
| 322 | $params = \array_merge([$date], $ids, [$userId]); |
||
| 323 | return $this->execute($sql, $params)->rowCount(); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function latestInsertTime(string $userId) : ?\DateTime { |
||
| 327 | $sql = "SELECT MAX(`{$this->getTableName()}`.`created`) FROM `{$this->getTableName()}` WHERE `user_id` = ?"; |
||
| 328 | $result = $this->execute($sql, [$userId]); |
||
| 329 | $createdTime = $result->fetch(\PDO::FETCH_COLUMN); |
||
| 330 | |||
| 331 | return ($createdTime === null) ? null : new \DateTime($createdTime); |
||
| 332 | } |
||
| 333 | |||
| 334 | public function latestUpdateTime(string $userId) : ?\DateTime { |
||
| 335 | $sql = "SELECT MAX(`{$this->getTableName()}`.`updated`) FROM `{$this->getTableName()}` WHERE `user_id` = ?"; |
||
| 336 | $result = $this->execute($sql, [$userId]); |
||
| 337 | $createdTime = $result->fetch(\PDO::FETCH_COLUMN); |
||
| 338 | |||
| 339 | return ($createdTime === null) ? null : new \DateTime($createdTime); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * helper creating a string like '(?,?,?)' with the specified number of elements |
||
| 344 | */ |
||
| 345 | protected function questionMarks(int $count) : string { |
||
| 346 | $questionMarks = []; |
||
| 347 | for ($i = 0; $i < $count; $i++) { |
||
| 348 | $questionMarks[] = '?'; |
||
| 349 | } |
||
| 350 | return '(' . \implode(',', $questionMarks) . ')'; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Build a SQL SELECT statement which selects all entities of the given user, |
||
| 355 | * and optionally applies other conditions, too. |
||
| 356 | * This is built upon `selectEntities` which may be overridden by the derived class. |
||
| 357 | * @param string|null $condition Optional extra condition. This will get automatically |
||
| 358 | * prefixed with ' AND ', so don't include that. |
||
| 359 | * @param string|null $extension Any extension (e.g. ORDER BY, LIMIT) to be added after |
||
| 360 | * the conditions in the SQL statement |
||
| 361 | */ |
||
| 362 | protected function selectUserEntities(string $condition=null, string $extension=null) : string { |
||
| 363 | $allConditions = "`{$this->getTableName()}`.`user_id` = ?"; |
||
| 364 | |||
| 365 | if (!empty($condition)) { |
||
| 366 | $allConditions .= " AND $condition"; |
||
| 367 | } |
||
| 368 | |||
| 369 | return $this->selectEntities($allConditions, $extension); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Build a SQL SELECT statement which selects all entities matching the given condition. |
||
| 374 | * The derived class may override this if necessary. |
||
| 375 | * @param string $condition This will get automatically prefixed with ' WHERE ' |
||
| 376 | * @param string|null $extension Any extension (e.g. ORDER BY, LIMIT) to be added after |
||
| 377 | * the conditions in the SQL statement |
||
| 378 | */ |
||
| 379 | protected function selectEntities(string $condition, string $extension=null) : string { |
||
| 380 | return "SELECT * FROM `{$this->getTableName()}` WHERE $condition $extension "; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return array with two values: The SQL condition as string and the SQL parameters as string[] |
||
| 385 | */ |
||
| 386 | protected function formatTimestampConditions(?string $createdMin, ?string $createdMax, ?string $updatedMin, ?string $updatedMax) : array { |
||
| 387 | $conditions = []; |
||
| 388 | $params = []; |
||
| 389 | |||
| 390 | if (!empty($createdMin)) { |
||
| 391 | $conditions[] = "`{$this->getTableName()}`.`created` >= ?"; |
||
| 392 | $params[] = $createdMin; |
||
| 393 | } |
||
| 394 | |||
| 395 | if (!empty($createdMax)) { |
||
| 396 | $conditions[] = "`{$this->getTableName()}`.`created` <= ?"; |
||
| 397 | $params[] = $createdMax; |
||
| 398 | } |
||
| 399 | |||
| 400 | if (!empty($updatedMin)) { |
||
| 401 | $conditions[] = "`{$this->getTableName()}`.`updated` >= ?"; |
||
| 402 | $params[] = $updatedMin; |
||
| 403 | } |
||
| 404 | |||
| 405 | if (!empty($updatedMax)) { |
||
| 406 | $conditions[] = "`{$this->getTableName()}`.`updated` <= ?"; |
||
| 407 | $params[] = $updatedMax; |
||
| 408 | } |
||
| 409 | |||
| 410 | return [\implode(' AND ', $conditions), $params]; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Convert given sorting condition to an SQL clause. Derived class may overide this if necessary. |
||
| 415 | * @param int $sortBy One of the constants defined in the class SortBy |
||
| 416 | */ |
||
| 417 | protected function formatSortingClause(int $sortBy) : ?string { |
||
| 418 | if ($sortBy == SortBy::Name) { |
||
| 419 | return "ORDER BY LOWER(`{$this->getTableName()}`.`{$this->nameColumn}`)"; |
||
| 420 | } elseif ($sortBy == SortBy::Newest) { |
||
| 421 | return "ORDER BY `{$this->getTableName()}`.`id` DESC"; // abuse the fact that IDs are ever-incrementing values |
||
| 422 | } else { |
||
| 423 | return null; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | protected static function prepareFuzzySearchString(string $input) : string { |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Find an entity which has the same identity as the supplied entity. |
||
| 445 | * How the identity of the entity is defined, depends on the derived concrete class. |
||
| 446 | * @phpstan-param EntityType $entity |
||
| 447 | * @phpstan-return EntityType |
||
| 448 | */ |
||
| 449 | abstract protected function findUniqueEntity(Entity $entity) : Entity; |
||
| 450 | } |
||
| 451 |