Failed Conditions
Pull Request — master (#72)
by Sander
03:09
created

NotebookApiController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 135
Duplicated Lines 17.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 5
dl 24
loc 135
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A index() 0 10 2
A get() 9 9 2
A create() 0 20 4
B update() 0 26 5
A delete() 15 15 3
A formatApiResponse() 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 - NextNote
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\NextNote\Controller;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Fixtures\ShareFix;
28
use OCA\NextNote\Service\NotebookService;
29
use OCA\NextNote\Service\NoteService;
30
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
31
use OCA\NextNote\Utility\NotFoundJSONResponse;
32
use OCA\NextNote\Utility\UnauthorizedJSONResponse;
33
use OCA\NextNote\Utility\Utils;
34
use \OCP\AppFramework\ApiController;
35
use OCP\AppFramework\Http\JSONResponse;
36
use OCP\Constants;
37
use OCP\IConfig;
38
use OCP\ILogger;
39
use \OCP\IRequest;
40
use OCP\IUserManager;
41
use OCP\Share;
42
43
44
class NotebookApiController extends ApiController {
45
46
	private $config;
47
	private $groupService;
48
	private $shareBackend;
49
	private $userManager;
50
51
	public function __construct($appName, IRequest $request,
52
								ILogger $logger, IConfig $config, NotebookService $noteService, NextNoteShareBackend $shareBackend, IUserManager $userManager) {
53
		parent::__construct($appName, $request);
54
		$this->config = $config;
55
		$this->groupService = $noteService;
56
		$this->shareBackend = $shareBackend;
57
		$this->userManager = $userManager;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 * @TODO Add etag / lastmodified
64
	 * @param int|bool $deleted
65
	 * @param string|bool $group
66
	 * @return JSONResponse
67
	 */
68
	public function index($deleted = false, $group = false) {
69
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
70
		$results = $this->groupService->find(null, $uid);
71
		foreach ($results as &$group) {
72
			$group = $group->jsonSerialize();
73
			$this->formatApiResponse($group);
0 ignored issues
show
Unused Code introduced by
The call to the method OCA\NextNote\Controller\...er::formatApiResponse() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
74
75
		}
76
		return new JSONResponse($results);
77
	}
78
79
	/**
80
	 * @NoAdminRequired
81
	 * @NoCSRFRequired
82
	 * @TODO Add etag / lastmodified
83
	 */
84 View Code Duplication
	public function get($id) {
85
		$result = $this->groupService->find($id);
86
		if (!$result) {
87
			return new NotFoundJSONResponse();
88
		}
89
		//@todo Check access
90
		$result = $result->jsonSerialize();
91
		return new JSONResponse($this->formatApiResponse($result));
92
	}
93
94
95
	/**
96
	 * @NoAdminRequired
97
	 * @NoCSRFRequired
98
	 */
99
	public function create($name, $color, $parent_id) {
100
		if ($name == "" || !$name) {
101
			return new JSONResponse(['error' => 'name is missing']);
102
		}
103
		$group = [
104
			'parent_id' => $parent_id,
105
			'name' => $name,
106
			'color' => $color,
107
			'guid' => Utils::GUID()
108
		];
109
110
		if($this->groupService->findByName($name)){
111
			return new JSONResponse(['error' => 'Group already exists']);
112
		}
113
114
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
115
		$result = $this->groupService->create($group, $uid)->jsonSerialize();
116
		\OC_Hook::emit('OCA\NextNote', 'post_create_group', ['group' => $group]);
117
		return new JSONResponse($this->formatApiResponse($result));
118
	}
119
120
	/**
121
	 * @NoAdminRequired
122
	 * @NoCSRFRequired
123
	 */
124
	public function update($id, $name, $color, $parent_id) {
125
		if ($name == "" || !$name) {
126
			return new JSONResponse(['error' => 'title is missing']);
127
		}
128
129
130
		$group = [
131
			'parent_id' => $parent_id,
132
			'name' => $name,
133
			'color' => $color,
134
		];
135
		//@TODO for sharing add access check
136
		$entity = $this->groupService->find($id);
137
		if (!$entity) {
138
			return new NotFoundJSONResponse();
139
		}
140
141
142
		if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_UPDATE, $entity)) {
143
			return new UnauthorizedJSONResponse();
144
		}
145
146
		$results = $this->groupService->update($group)->jsonSerialize();
147
		\OC_Hook::emit('OCA\NextNote', 'post_update_group', ['group' => $group]);
148
		return new JSONResponse($this->formatApiResponse($results));
149
	}
150
151
	/**
152
	 * @NoAdminRequired
153
	 * @NoCSRFRequired
154
	 */
155 View Code Duplication
	public function delete($id) {
156
		$entity = $this->groupService->find($id);
157
		if (!$entity) {
158
			return new NotFoundJSONResponse();
159
		}
160
161
		if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_DELETE, $entity)) {
162
			return new UnauthorizedJSONResponse();
163
		}
164
165
		$this->groupService->delete($id);
166
		$result = (object)['success' => true];
167
		\OC_Hook::emit('OCA\NextNote', 'post_delete_group', ['group_id' => $id]);
168
		return new JSONResponse($result);
169
	}
170
171
	/**
172
	 * @param $group array
173
	 * @return array
174
	 */
175
	private function formatApiResponse($group) {
176
		return $group;
177
	}
178
}
179