Failed Conditions
Pull Request — master (#6)
by Sander
01:46
created

Ownnotev2ApiController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 17.98 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 16
loc 89
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 0 4 1
A get() 7 7 2
A create() 0 10 1
A update() 0 17 2
A delete() 9 9 2

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 - ownnote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
6
 * @copyright Copyright (c) 2017, Sander Brand ([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
namespace OCA\OwnNote\Controller;
25
26
use OCA\OwnNote\Service\OwnNoteService;
27
use OCA\OwnNote\Utility\NotFoundJSONResponse;
28
use \OCP\AppFramework\ApiController;
29
use OCP\AppFramework\Http\JSONResponse;
30
use OCP\IConfig;
31
use OCP\ILogger;
32
use \OCP\IRequest;
33
use \OCA\OwnNote\Lib\Backend;
34
35
36
class Ownnotev2ApiController extends ApiController {
37
38
	private $backend;
39
	private $config;
40
	private $noteService;
41
42
	public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, OwnNoteService $noteService){
43
		parent::__construct($appName, $request);
44
		$this->backend = new Backend($config);
45
		$this->config = $config;
46
		$this->noteService = $noteService;
47
	}
48
49
	/**
50
	* MOBILE FUNCTIONS
51
	*/
52
53
	/**
54
	* @NoAdminRequired
55
	* @NoCSRFRequired
56
	*/
57
	public function index() {
58
		$results = $this->noteService->getListing('', false);
59
		return new JSONResponse($results);
60
	}
61
	/**
62
	* @NoAdminRequired
63
	* @NoCSRFRequired
64
	*/
65 View Code Duplication
	public function get($id) {
66
		$results = $this->noteService->find($id);
67
		if(!$results){
68
			return new NotFoundJSONResponse();
69
		}
70
		return new JSONResponse($results);
71
	}
72
73
74
	/**
75
	* @NoAdminRequired
76
	* @NoCSRFRequired
77
	*/
78
	public function create($title, $group, $note) {
79
		$note = [
80
			'title' => $title,
81
			'group' => $group,
82
			'note' => $note
83
		];
84
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
85
		$result = $this->noteService->create('', $note, $uid); //@TODO add folder
86
		return new JSONResponse($result);
87
	}
88
89
	/**
90
	* @NoAdminRequired
91
	* @NoCSRFRequired
92
	*/
93
	public function update($id, $title, $group, $note) {
94
95
		$note = [
96
			'id' => $id,
97
			'title' => $title,
98
			'group' => $group,
99
			'note' => $note
100
		];
101
102
		$entity = $this->noteService->find($id);
103
		if(!$entity){
104
			return new NotFoundJSONResponse();
105
		}
106
107
		$results = $this->noteService->update('',$note); //@TODO add folder
108
		return new JSONResponse($results);
109
	}
110
	/**
111
	* @NoAdminRequired
112
	* @NoCSRFRequired
113
	*/
114 View Code Duplication
	public function delete($id) {
115
		$entity = $this->noteService->find($id);
116
		if(!$entity){
117
			return new NotFoundJSONResponse();
118
		}
119
120
		$results = $this->noteService->delete('', $id); //@TODO add folder
121
		return new JSONResponse($results);
122
	}
123
124
}
125