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 |
||
| 31 | use OCP\AppFramework\Db\QBMapper; |
||
| 32 | use OCP\DB\QueryBuilder\IQueryBuilder; |
||
| 33 | use OCP\IDBConnection; |
||
| 34 | |||
| 35 | class FileMapper extends QBMapper { |
||
| 36 | const TABLE_NAME = 'passman_files'; |
||
| 37 | private Utils $utils; |
||
|
|
|||
| 38 | |||
| 39 | public function __construct(IDBConnection $db, Utils $utils) { |
||
| 40 | parent::__construct($db, self::TABLE_NAME); |
||
| 41 | $this->utils = $utils; |
||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int $file_id |
||
| 47 | * @param string|null $user_id |
||
| 48 | * @return Entity |
||
| 49 | * @throws DoesNotExistException |
||
| 50 | * @throws MultipleObjectsReturnedException |
||
| 51 | */ |
||
| 52 | public function getFile(int $file_id, string $user_id = null) { |
||
| 53 | $qb = $this->db->getQueryBuilder(); |
||
| 54 | $qb->select('*') |
||
| 55 | ->from(self::TABLE_NAME) |
||
| 56 | ->where($qb->expr()->eq('id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))); |
||
| 57 | |||
| 58 | if ($user_id !== null) { |
||
| 59 | $qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $this->findEntity($qb); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $file_guid |
||
| 67 | * @param string|null $user_id |
||
| 68 | * @return Entity |
||
| 69 | * @throws DoesNotExistException |
||
| 70 | * @throws MultipleObjectsReturnedException |
||
| 71 | */ |
||
| 72 | public function getFileByGuid(string $file_guid, string $user_id = null) { |
||
| 73 | $qb = $this->db->getQueryBuilder(); |
||
| 74 | $qb->select('*') |
||
| 75 | ->from(self::TABLE_NAME) |
||
| 76 | ->where($qb->expr()->eq('guid', $qb->createNamedParameter($file_guid, IQueryBuilder::PARAM_STR))); |
||
| 77 | |||
| 78 | if ($user_id !== null) { |
||
| 79 | $qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))); |
||
| 80 | } |
||
| 81 | |||
| 82 | return $this->findEntity($qb); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $file_raw |
||
| 87 | * @param $userId |
||
| 88 | * @return File |
||
| 89 | */ |
||
| 90 | public function create($file_raw, $userId) { |
||
| 91 | $file = new File(); |
||
| 92 | $file->setGuid($this->utils->GUID()); |
||
| 93 | $file->setUserId($userId); |
||
| 94 | $file->setFilename($file_raw['filename']); |
||
| 95 | $file->setSize($file_raw['size']); |
||
| 96 | $file->setCreated($this->utils->getTime()); |
||
| 97 | $file->setFileData($file_raw['file_data']); |
||
| 98 | $file->setMimetype($file_raw['mimetype']); |
||
| 99 | |||
| 100 | return $this->insert($file); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Delete a file by file_id and user id |
||
| 105 | * |
||
| 106 | * @param int $file_id |
||
| 107 | * @param string $userId |
||
| 108 | * @return File|Entity |
||
| 109 | */ |
||
| 110 | public function deleteFile(int $file_id, string $userId) { |
||
| 111 | $file = new File(); |
||
| 112 | $file->setId($file_id); |
||
| 113 | $file->setUserId($userId); |
||
| 114 | return $this->delete($file); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Uodate file |
||
| 119 | * @param File $file |
||
| 120 | * @return File |
||
| 121 | */ |
||
| 122 | public function updateFile(File $file) { |
||
| 123 | return $this->update($file); |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $user_id |
||
| 129 | * @return Entity[] |
||
| 130 | */ |
||
| 131 | public function getFilesFromUser(string $user_id) { |
||
| 140 |