Code Duplication    Length = 4-5 lines in 6 locations

lib/Controller/PaperHiveController.php 6 locations

@@ 213-216 (lines=4) @@
210
	public function generatePaperHiveDocument($dir, $bookID) {
211
		// Try to get the document
212
		$paperHiveObjectString = $this->fetchDocument($bookID);
213
		if ($paperHiveObjectString === false) {
214
			$message = (string)$this->l->t('Problem connecting to PaperHive.');
215
			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
216
		}
217
		$paperHiveObject = json_decode($paperHiveObjectString, true);
218
219
		// Check if correct response has been returned
@@ 220-223 (lines=4) @@
217
		$paperHiveObject = json_decode($paperHiveObjectString, true);
218
219
		// Check if correct response has been returned
220
		if (json_last_error() != JSON_ERROR_NONE) {
221
			$message = (string)$this->l->t('Received wrong response from PaperHive.');
222
			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
223
		}
224
225
		// Check if document is found
226
		if (!(isset($paperHiveObject['metadata']) && isset($paperHiveObject['metadata']['title']))) {
@@ 233-236 (lines=4) @@
230
231
		// Try fetching discussions
232
		$paperHiveDiscussionsString = $this->fetchDiscussions($bookID);
233
		if ($paperHiveDiscussionsString === false) {
234
			$message = (string)$this->l->t('Problem connecting to PaperHive.');
235
			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
236
		}
237
		$paperHiveDiscussions = json_decode($paperHiveDiscussionsString, true);
238
		$discussionCount = -1;
239
		if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) {
@@ 253-256 (lines=4) @@
250
		}
251
252
		$exists = $this->view->file_exists($path);
253
		if ($exists) {
254
			$message = (string) $this->l->t('The file already exists.');
255
			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
256
		}
257
258
		try {
259
			$created = $this->view->touch($path);
@@ 260-263 (lines=4) @@
257
258
		try {
259
			$created = $this->view->touch($path);
260
			if(!$created) {
261
				$message = (string) $this->l->t('Could not save document.');
262
				return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
263
			}
264
265
			$fileInfo = $this->view->getFileInfo($path);
266
			$inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID);
@@ 267-271 (lines=5) @@
264
265
			$fileInfo = $this->view->getFileInfo($path);
266
			$inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID);
267
			if(!$inserted) {
268
				$this->view->unlink($path);
269
				$message = (string) $this->l->t('Could not save document metadata.');
270
				return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
271
			}
272
		} catch (LockedException $e) {
273
			$message = (string) $this->l->t('The file is locked.');
274
			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);