Completed
Push — master ( 6135d2...dffc50 )
by Sander
14:38 queued 12:08
created

FileController::updateFile()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 4
cts 4
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 18
nop 3
crap 7
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Sander Brand <[email protected]>
9
 * @copyright Sander Brand 2016
10
 */
11
12
namespace OCA\Passman\Controller;
13
14
use OCP\AppFramework\Db\DoesNotExistException;
15
use OCP\IRequest;
16
use OCP\AppFramework\Http\JSONResponse;
17
use OCP\AppFramework\ApiController;
18
use OCA\Passman\Service\FileService;
19
20
class FileController extends ApiController {
21
	private $userId;
22
	private $fileService;
23
	public function __construct($AppName,
24
								IRequest $request,
25
								$UserId,
26
								FileService $fileService){
27
		parent::__construct($AppName, $request);
28
		$this->userId = $UserId;
29
		$this->fileService = $fileService;
30
	}
31
32
33
	/**
34
	 * @NoAdminRequired
35
	 * @NoCSRFRequired
36
	 */
37 1
	public function uploadFile($data, $filename, $mimetype, $size) {
38
		$file = array(
39 1
			'filename' => $filename,
40 1
			'size' => $size,
41 1
			'mimetype' => $mimetype,
42 1
			'file_data' => $data
43
		);
44 1
		return new JSONResponse($this->fileService->createFile($file, $this->userId));
45
	}
46
47
	/**
48
	 * @NoAdminRequired
49
	 * @NoCSRFRequired
50
	 */
51 1
	public function getFile($file_id) {
52 1
		return new JSONResponse($this->fileService->getFile($file_id, $this->userId));
53
	}
54
	/**
55
	 * @NoAdminRequired
56
	 * @NoCSRFRequired
57
	 */
58 1
	public function deleteFile($file_id) {
59 1
		return new JSONResponse($this->fileService->deleteFile($file_id, $this->userId));
60
	}
61
62 1
	public function updateFile($file_id, $file_data, $filename){
63
		try{
64 1
			$file = $this->fileService->getFile($file_id, $this->userId);
65
		} catch (\Exception $doesNotExistException){
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
66
67
		}
68 1
		if($file){
69
			if($file_data) {
70
				$file->setFileData($file_data);
71
			}
72
			if($filename) {
73
				$file->setFilename($filename);
74
			}
75
			if($filename || $file_data){
76
				new JSONResponse($this->fileService->updateFile($file));
77
			}
78
		}
79
	}
80
}