Completed
Branch master (8ca3ab)
by Sander
03:07
created

FileMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 2
dl 20
loc 85
ccs 32
cts 32
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 1
A __construct() 0 4 1
A getFile() 10 10 2
A getFileByGuid() 10 10 2
A deleteFile() 0 6 1
A updateFile() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
25
namespace OCA\Passman\Db;
26
27
use OCA\Passman\Utility\Utils;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\Mapper;
30
31
class FileMapper extends Mapper {
32
	private $utils;
33
34
	public function __construct(IDBConnection $db, Utils $utils) {
35
		parent::__construct($db, 'passman_files');
36
		$this->utils = $utils;
37
	}
38
39
40
	/**
41
	 * @param $file_id
42
	 * @param null $user_id
43
	 * @return File
44
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
45
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
46
	 */
47 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...
48
		$sql = 'SELECT * FROM `*PREFIX*passman_files` ' .
49 1
			'WHERE `id` = ?';
50 1
		$params = [$file_id];
51 1
		if ($user_id !== null) {
52 1
			$sql .= ' and `user_id` = ? ';
53 1
			array_push($params, $user_id);
54
		}
55 1
		return $this->findEntity($sql, $params);
56
	}
57
	/**
58
	 * @param $file_id
59
	 * @param null $user_id
60
	 * @return File
61
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
62
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
63
	 */
64 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...
65
		$sql = 'SELECT * FROM `*PREFIX*passman_files` ' .
66 1
			'WHERE `guid` = ?';
67 1
		$params = [$file_guid];
68 1
		if ($user_id !== null) {
69 1
			$sql .= ' and `user_id` = ? ';
70 1
			array_push($params, $user_id);
71
		}
72 1
		return $this->findEntity($sql, $params);
73
	}
74
75
	/**
76
	 * @param $file_raw
77
	 * @param $userId
78
	 * @return File
79
	 */
80 1
	public function create($file_raw, $userId) {
81 1
		$file = new File();
82 1
		$file->setGuid($this->utils->GUID());
83 1
		$file->setUserId($userId);
84 1
		$file->setFilename($file_raw['filename']);
85 1
		$file->setSize($file_raw['size']);
86 1
		$file->setCreated($this->utils->getTime());
87 1
		$file->setFileData($file_raw['file_data']);
88 1
		$file->setMimetype($file_raw['mimetype']);
89
90
91 1
		return $this->insert($file);
92
	}
93
94
	/**
95
	 * Delete a file by file_id and user id
96
	 * @param $file_id
97
	 * @param $userId
98
	 * @return File
99
	 */
100 1
	public function deleteFile($file_id, $userId) {
101 1
		$file = new File();
102 1
		$file->setId($file_id);
103 1
		$file->setUserId($userId);
104 1
		$this->delete($file);
105 1
	}
106
107
	/**
108
	 * Uodate file
109
	 * @param File $file
110
	 * @return File
111
	 */
112 1
	public function updateFile(File $file) {
113 1
		return $this->update($file);
114
	}
115
}