Completed
Push — master ( 75f541...50b854 )
by Marcos
02:58
created

FileMapper::updateFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
namespace OCA\Passman\Db;
12
13
use OCA\Passman\Utility\Utils;
14
use OCP\IDBConnection;
15
use OCP\AppFramework\Db\Mapper;
16
17
class FileMapper extends Mapper {
18
	private $utils;
19
20
	public function __construct(IDBConnection $db, Utils $utils) {
21
		parent::__construct($db, 'passman_files');
22
		$this->utils = $utils;
23
	}
24
25
26
	/**
27
	 * @param $file_id
28
	 * @param null $user_id
29
	 * @return File
30
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
31
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
32
	 */
33 1 View Code Duplication
	public function getFile($file_id, $user_id = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
		$sql = 'SELECT * FROM `*PREFIX*passman_files` ' .
35 1
			'WHERE `id` = ?';
36 1
		$params = [$file_id];
37 1
		if ($user_id !== null) {
38 1
			$sql .= ' and `user_id` = ? ';
39 1
			array_push($params, $user_id);
40 1
		}
41 1
		return $this->findEntity($sql, $params);
42
	}
43
	/**
44
	 * @param $file_id
45
	 * @param null $user_id
46
	 * @return File
47
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
48
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
49
	 */
50 1 View Code Duplication
	public function getFileByGuid($file_guid, $user_id = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
		$sql = 'SELECT * FROM `*PREFIX*passman_files` ' .
52 1
			'WHERE `guid` = ?';
53 1
		$params = [$file_guid];
54 1
		if ($user_id !== null) {
55 1
			$sql .= ' and `user_id` = ? ';
56 1
			array_push($params, $user_id);
57 1
		}
58 1
		return $this->findEntity($sql, $params);
59
	}
60
61
	/**
62
	 * @param $file_raw
63
	 * @param $userId
64
	 * @return File
65
	 */
66 1
	public function create($file_raw, $userId) {
67 1
		$file = new File();
68 1
		$file->setGuid($this->utils->GUID());
69 1
		$file->setUserId($userId);
70 1
		$file->setFilename($file_raw['filename']);
71 1
		$file->setSize($file_raw['size']);
72 1
		$file->setCreated($this->utils->getTime());
73 1
		$file->setFileData($file_raw['file_data']);
74 1
		$file->setMimetype($file_raw['mimetype']);
75
76
77 1
		return $this->insert($file);
78
	}
79
80 1
	public function deleteFile($file_id, $userId) {
81 1
		$file = new File();
82 1
		$file->setId($file_id);
83 1
		$file->setUserId($userId);
84 1
		$this->delete($file);
85 1
	}
86
87 1
	public function updateFile(File $file) {
88 1
		return $this->update($file);
89
	}
90
}