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 |
||
| 36 | class PaperHiveController extends Controller { |
||
| 37 | |||
| 38 | /** @var IL10N */ |
||
| 39 | private $l; |
||
| 40 | |||
| 41 | /** @var View */ |
||
| 42 | private $view; |
||
| 43 | |||
| 44 | /** @var ILogger */ |
||
| 45 | private $logger; |
||
| 46 | |||
| 47 | /** @var \OCP\Http\Client\IClient */ |
||
| 48 | private $client; |
||
| 49 | |||
| 50 | /** @var PaperHiveMetadata */ |
||
| 51 | private $paperHiveMetadata; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Paperhive base URL |
||
| 55 | */ |
||
| 56 | private $paperhive_base_url = 'https://paperhive.org'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Paperhive url for document API |
||
| 60 | */ |
||
| 61 | private $paperhive_api_documents = '/api/document-items/'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Paperhive url for discussions API |
||
| 65 | */ |
||
| 66 | private $paperhive_api_discussions = '/api/discussions?documentItem='; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Paperhive url for document text in browser |
||
| 70 | */ |
||
| 71 | private $paperhive_base_document_url = '/documents/items/'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Paperhive file extension |
||
| 75 | */ |
||
| 76 | private $paperhive_file_extension = '.paperhive'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Paperhive BookID example |
||
| 80 | */ |
||
| 81 | private $paperhive_bookid_example = 'ZYY0r21rJbqr'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @NoAdminRequired |
||
| 85 | * |
||
| 86 | * @param string $AppName |
||
| 87 | * @param IRequest $request |
||
| 88 | * @param IL10N $l10n |
||
| 89 | * @param View $view |
||
| 90 | * @param ILogger $logger |
||
| 91 | * @param IClient $client |
||
| 92 | * @param PaperHiveMetadata $paperHiveMetadata |
||
| 93 | */ |
||
| 94 | public function __construct($AppName, |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get URL to PaperHive book url |
||
| 111 | * |
||
| 112 | * @NoAdminRequired |
||
| 113 | * |
||
| 114 | * @param string $dir |
||
| 115 | * @param string $filename |
||
| 116 | * @return DataResponse |
||
| 117 | */ |
||
| 118 | public function getPaperHiveBookURL($dir, $filename) { |
||
| 119 | $bookId = $this->getBookIdforPath($dir, $filename); |
||
| 120 | if (!$bookId) { |
||
|
|
|||
| 121 | $message = (string)$this->l->t('No such document found in database.'); |
||
| 122 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 123 | } |
||
| 124 | return new DataResponse($this->paperhive_base_url . $this->paperhive_base_document_url . $bookId, Http::STATUS_OK); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get URL to PaperHive book discussion count |
||
| 129 | * |
||
| 130 | * @NoAdminRequired |
||
| 131 | * |
||
| 132 | * @param string $dir |
||
| 133 | * @param string $filename |
||
| 134 | * @return DataResponse |
||
| 135 | */ |
||
| 136 | public function getPaperHiveBookDiscussionCount($dir, $filename) { |
||
| 137 | $bookId = $this->getBookIdforPath($dir, $filename); |
||
| 138 | $paperHiveString = $this->fetchDiscussions($bookId); |
||
| 139 | $paperHiveDiscussions = json_decode($paperHiveString, true); |
||
| 140 | $disscussionCount = -1; |
||
| 141 | View Code Duplication | if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) { |
|
| 142 | // Silently ignore discussions as this might indicate temporary unavailability |
||
| 143 | $disscussionCount = count($paperHiveDiscussions['discussions']); |
||
| 144 | } |
||
| 145 | return new DataResponse($disscussionCount, Http::STATUS_OK); |
||
| 146 | } |
||
| 147 | |||
| 148 | private function getBookIdforPath($dir, $filename){ |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Does the call to PaperHive Discussions API and returns disussions for specific it for specific BookID |
||
| 166 | * |
||
| 167 | * @NoAdminRequired |
||
| 168 | * |
||
| 169 | * @param string $bookID |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | View Code Duplication | private function fetchDiscussions($bookID) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Does the call to PaperHive Documents API and returns the JSON for a book for specific BookID |
||
| 184 | * |
||
| 185 | * @NoAdminRequired |
||
| 186 | * |
||
| 187 | * @param string $bookID |
||
| 188 | * @return string/boolean |
||
| 189 | */ |
||
| 190 | View Code Duplication | private function fetchDocument($bookID) { |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Gets the informations about the book for specific BookID and saves as a file |
||
| 202 | * |
||
| 203 | * @NoAdminRequired |
||
| 204 | * |
||
| 205 | * @param string $dir |
||
| 206 | * @param string $bookID |
||
| 207 | * @return DataResponse |
||
| 208 | */ |
||
| 209 | public function generatePaperHiveDocument($dir, $bookID) { |
||
| 210 | // Try to get the document |
||
| 211 | $paperHiveObjectString = $this->fetchDocument($bookID); |
||
| 212 | View Code Duplication | if ($paperHiveObjectString === false) { |
|
| 213 | $message = (string)$this->l->t('Problem connecting to PaperHive.'); |
||
| 214 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 215 | } |
||
| 216 | $paperHiveObject = json_decode($paperHiveObjectString, true); |
||
| 217 | |||
| 218 | // Check if correct response has been returned |
||
| 219 | View Code Duplication | if (json_last_error() != JSON_ERROR_NONE) { |
|
| 220 | $message = (string)$this->l->t('Received wrong response from PaperHive.'); |
||
| 221 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 222 | } |
||
| 223 | |||
| 224 | // Check if document is found |
||
| 225 | if (!(isset($paperHiveObject['metadata']) && isset($paperHiveObject['metadata']['title']))) { |
||
| 226 | $message = (string)$this->l->t('Document with this BookID cannot be found'); |
||
| 227 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Try fetching discussions |
||
| 231 | $paperHiveDiscussionsString = $this->fetchDiscussions($bookID); |
||
| 232 | $paperHiveDiscussions = json_decode($paperHiveDiscussionsString, true); |
||
| 233 | View Code Duplication | if ($paperHiveDiscussionsString === false || json_last_error() != JSON_ERROR_NONE) { |
|
| 234 | $message = (string)$this->l->t('Problem connecting to PaperHive to fetch discussions.'); |
||
| 235 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 236 | } |
||
| 237 | $discussionCount = -1; |
||
| 238 | View Code Duplication | if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) { |
|
| 239 | $discussionCount = count($paperHiveDiscussions['discussions']); |
||
| 240 | } |
||
| 241 | |||
| 242 | // Save the file |
||
| 243 | $title = $paperHiveObject['metadata']['title']; |
||
| 244 | $filename = $title . $this->paperhive_file_extension; |
||
| 245 | View Code Duplication | if($dir == '/') { |
|
| 246 | $path = $dir . $filename; |
||
| 247 | } else { |
||
| 248 | $path = $dir . '/' . $filename; |
||
| 249 | } |
||
| 250 | |||
| 251 | $exists = $this->view->file_exists($path); |
||
| 252 | if ($exists) { |
||
| 253 | $message = (string) $this->l->t('The file already exists.'); |
||
| 254 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 255 | } |
||
| 256 | |||
| 257 | try { |
||
| 258 | $created = $this->view->touch($path); |
||
| 259 | if(!$created) { |
||
| 260 | $message = (string) $this->l->t('Could not save document.'); |
||
| 261 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 262 | } |
||
| 263 | |||
| 264 | $fileInfo = $this->view->getFileInfo($path); |
||
| 265 | $inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID); |
||
| 266 | if(!$inserted) { |
||
| 267 | $this->view->unlink($path); |
||
| 268 | $message = (string) $this->l->t('Could not save document metadata.'); |
||
| 269 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 270 | } |
||
| 271 | } catch (LockedException $e) { |
||
| 272 | $message = (string) $this->l->t('The file is locked.'); |
||
| 273 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 274 | } catch (ForbiddenException $e) { |
||
| 275 | return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
||
| 276 | } catch (\Exception $e) { |
||
| 277 | $message = (string)$this->l->t('An internal server error occurred.'); |
||
| 278 | return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
||
| 279 | } |
||
| 280 | |||
| 281 | return new DataResponse(['path' => $path, 'filename' => $title, 'extension' => $this->paperhive_file_extension, 'discussionCount' => $discussionCount], Http::STATUS_OK); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns all required PaperHive setting |
||
| 286 | * |
||
| 287 | * @NoAdminRequired |
||
| 288 | * |
||
| 289 | * @return DataResponse |
||
| 290 | */ |
||
| 291 | public function getPaperHiveDetails() { |
||
| 301 | } |
||
| 302 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: