Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 19 | class DoctrineDatabase extends Gateway |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Database handler. |
||
| 23 | * |
||
| 24 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
| 25 | */ |
||
| 26 | protected $handler; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Construct from database handler. |
||
| 30 | * |
||
| 31 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $handler |
||
| 32 | */ |
||
| 33 | public function __construct(DatabaseHandler $handler) |
||
| 34 | { |
||
| 35 | $this->handler = $handler; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create user. |
||
| 40 | * |
||
| 41 | * @param user $user |
||
| 42 | * |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | public function createUser(User $user) |
||
| 46 | { |
||
| 47 | $query = $this->handler->createInsertQuery(); |
||
| 48 | $query |
||
| 49 | ->insertInto($this->handler->quoteTable('ezuser')) |
||
| 50 | ->set( |
||
| 51 | $this->handler->quoteColumn('contentobject_id'), |
||
| 52 | $query->bindValue($user->id, null, \PDO::PARAM_INT) |
||
| 53 | )->set( |
||
| 54 | $this->handler->quoteColumn('login'), |
||
| 55 | $query->bindValue($user->login) |
||
| 56 | )->set( |
||
| 57 | $this->handler->quoteColumn('email'), |
||
| 58 | $query->bindValue($user->email) |
||
| 59 | )->set( |
||
| 60 | $this->handler->quoteColumn('password_hash'), |
||
| 61 | $query->bindValue($user->passwordHash) |
||
| 62 | )->set( |
||
| 63 | $this->handler->quoteColumn('password_hash_type'), |
||
| 64 | $query->bindValue($user->hashAlgorithm, null, \PDO::PARAM_INT) |
||
| 65 | ); |
||
| 66 | $query->prepare()->execute(); |
||
| 67 | |||
| 68 | $query = $this->handler->createInsertQuery(); |
||
| 69 | $query |
||
| 70 | ->insertInto($this->handler->quoteTable('ezuser_setting')) |
||
| 71 | ->set( |
||
| 72 | $this->handler->quoteColumn('user_id'), |
||
| 73 | $query->bindValue($user->id, null, \PDO::PARAM_INT) |
||
| 74 | )->set( |
||
| 75 | $this->handler->quoteColumn('is_enabled'), |
||
| 76 | $query->bindValue($user->isEnabled, null, \PDO::PARAM_INT) |
||
| 77 | )->set( |
||
| 78 | $this->handler->quoteColumn('max_login'), |
||
| 79 | $query->bindValue($user->maxLogin, null, \PDO::PARAM_INT) |
||
| 80 | ); |
||
| 81 | $query->prepare()->execute(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Delete user with the given ID. |
||
| 86 | * |
||
| 87 | * @param mixed $userId |
||
| 88 | */ |
||
| 89 | public function deleteUser($userId) |
||
| 90 | { |
||
| 91 | $query = $this->handler->createDeleteQuery(); |
||
| 92 | $query |
||
| 93 | ->deleteFrom($this->handler->quoteTable('ezuser_setting')) |
||
| 94 | ->where( |
||
| 95 | $query->expr->eq( |
||
| 96 | $this->handler->quoteColumn('user_id'), |
||
| 97 | $query->bindValue($userId, null, \PDO::PARAM_INT) |
||
| 98 | ) |
||
| 99 | ); |
||
| 100 | $query->prepare()->execute(); |
||
| 101 | |||
| 102 | $query = $this->handler->createDeleteQuery(); |
||
| 103 | $query |
||
| 104 | ->deleteFrom($this->handler->quoteTable('ezuser')) |
||
| 105 | ->where( |
||
| 106 | $query->expr->eq( |
||
| 107 | $this->handler->quoteColumn('contentobject_id'), |
||
| 108 | $query->bindValue($userId, null, \PDO::PARAM_INT) |
||
| 109 | ) |
||
| 110 | ); |
||
| 111 | $query->prepare()->execute(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Loads user with user ID. |
||
| 116 | * |
||
| 117 | * @param mixed $userId |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function load($userId) |
|
| 122 | { |
||
| 123 | $query = $this->handler->createSelectQuery(); |
||
| 124 | $query->select( |
||
| 125 | $this->handler->quoteColumn('contentobject_id', 'ezuser'), |
||
| 126 | $this->handler->quoteColumn('login', 'ezuser'), |
||
| 127 | $this->handler->quoteColumn('email', 'ezuser'), |
||
| 128 | $this->handler->quoteColumn('password_hash', 'ezuser'), |
||
| 129 | $this->handler->quoteColumn('password_hash_type', 'ezuser'), |
||
| 130 | $this->handler->quoteColumn('is_enabled', 'ezuser_setting'), |
||
| 131 | $this->handler->quoteColumn('max_login', 'ezuser_setting') |
||
| 132 | )->from( |
||
| 133 | $this->handler->quoteTable('ezuser') |
||
| 134 | )->leftJoin( |
||
| 135 | $this->handler->quoteTable('ezuser_setting'), |
||
| 136 | $query->expr->eq( |
||
| 137 | $this->handler->quoteColumn('user_id', 'ezuser_setting'), |
||
| 138 | $this->handler->quoteColumn('contentobject_id', 'ezuser') |
||
| 139 | ) |
||
| 140 | )->where( |
||
| 141 | $query->expr->eq( |
||
| 142 | $this->handler->quoteColumn('contentobject_id', 'ezuser'), |
||
| 143 | $query->bindValue($userId, null, \PDO::PARAM_INT) |
||
| 144 | ) |
||
| 145 | ); |
||
| 146 | |||
| 147 | $statement = $query->prepare(); |
||
| 148 | $statement->execute(); |
||
| 149 | |||
| 150 | return $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Loads user with user login. |
||
| 155 | * |
||
| 156 | * @param string $login |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function loadByLogin($login) |
|
| 161 | { |
||
| 162 | $query = $this->handler->createSelectQuery(); |
||
| 163 | $query->select( |
||
| 164 | $this->handler->quoteColumn('contentobject_id', 'ezuser'), |
||
| 165 | $this->handler->quoteColumn('login', 'ezuser'), |
||
| 166 | $this->handler->quoteColumn('email', 'ezuser'), |
||
| 167 | $this->handler->quoteColumn('password_hash', 'ezuser'), |
||
| 168 | $this->handler->quoteColumn('password_hash_type', 'ezuser'), |
||
| 169 | $this->handler->quoteColumn('is_enabled', 'ezuser_setting'), |
||
| 170 | $this->handler->quoteColumn('max_login', 'ezuser_setting') |
||
| 171 | )->from( |
||
| 172 | $this->handler->quoteTable('ezuser') |
||
| 173 | )->leftJoin( |
||
| 174 | $this->handler->quoteTable('ezuser_setting'), |
||
| 175 | $query->expr->eq( |
||
| 176 | $this->handler->quoteColumn('user_id', 'ezuser_setting'), |
||
| 177 | $this->handler->quoteColumn('contentobject_id', 'ezuser') |
||
| 178 | ) |
||
| 179 | )->where( |
||
| 180 | $query->expr->eq( |
||
| 181 | $query->expr->lower($this->handler->quoteColumn('login', 'ezuser')), |
||
| 182 | // Index is case in-sensitive, on some db's lowercase, so we lowercase $login |
||
| 183 | $query->bindValue(mb_strtolower($login, 'UTF-8'), null, \PDO::PARAM_STR) |
||
| 184 | ) |
||
| 185 | ); |
||
| 186 | |||
| 187 | $statement = $query->prepare(); |
||
| 188 | $statement->execute(); |
||
| 189 | |||
| 190 | return $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Loads user with user email. |
||
| 195 | * |
||
| 196 | * @param string $email |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function loadByEmail($email) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Loads a user with user hash key. |
||
| 234 | * |
||
| 235 | * @param string $hash |
||
| 236 | * |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | public function loadUserByToken($hash) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Update the user information specified by the user struct. |
||
| 285 | * |
||
| 286 | * @param User $user |
||
| 287 | */ |
||
| 288 | public function updateUser(User $user) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Update or insert the user token information specified by the user token struct. |
||
| 333 | * |
||
| 334 | * @param \eZ\Publish\SPI\Persistence\User\UserTokenUpdateStruct $userTokenUpdateStruct |
||
| 335 | */ |
||
| 336 | public function updateUserToken(UserTokenUpdateStruct $userTokenUpdateStruct) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Expires user token with user hash. |
||
| 391 | * |
||
| 392 | * @param string $hash |
||
| 393 | */ |
||
| 394 | View Code Duplication | public function expireUserToken($hash) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Assigns role to user with given limitation. |
||
| 413 | * |
||
| 414 | * @param mixed $contentId |
||
| 415 | * @param mixed $roleId |
||
| 416 | * @param array $limitation |
||
| 417 | */ |
||
| 418 | public function assignRole($contentId, $roleId, array $limitation) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Remove role from user or user group. |
||
| 445 | * |
||
| 446 | * @param mixed $contentId |
||
| 447 | * @param mixed $roleId |
||
| 448 | */ |
||
| 449 | View Code Duplication | public function removeRole($contentId, $roleId) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Remove role from user or user group, by assignment ID. |
||
| 471 | * |
||
| 472 | * @param mixed $roleAssignmentId |
||
| 473 | */ |
||
| 474 | public function removeRoleAssignmentById($roleAssignmentId) |
||
| 487 | } |
||
| 488 |