Completed
Pull Request — stable8 (#244)
by Blizzz
03:49
created

BookmarkController   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 235
Duplicated Lines 6.38 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 38
c 2
b 0
f 0
lcom 1
cbo 2
dl 15
loc 235
ccs 0
cts 139
cp 0
rs 8.3999

11 Methods

Rating   Name   Duplication   Size   Complexity  
A legacyGetBookmarks() 0 3 1
A __construct() 6 6 1
A getBookmarks() 0 20 3
B newBookmark() 3 20 7
A legacyEditBookmark() 0 7 2
B editBookmark() 3 21 7
A legacyDeleteBookmark() 0 3 1
A deleteBookmark() 0 11 3
A clickBookmark() 3 20 4
B importBookmark() 0 25 4
B exportBookmark() 0 28 5

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
/**
4
 * ownCloud - bookmarks
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Stefan Klemm <[email protected]>
10
 * @copyright Stefan Klemm 2014
11
 */
12
13
namespace OCA\Bookmarks\Controller\Rest;
14
15
use \OCP\IRequest;
16
use \OCP\AppFramework\ApiController;
17
use \OCP\AppFramework\Http\JSONResponse;
18
use \OCP\AppFramework\Http;
19
use \OCP\IDb;
20
use \OCA\Bookmarks\Controller\Lib\Bookmarks;
21
use \OCA\Bookmarks\Controller\Lib\ExportResponse;
22
23
class BookmarkController extends ApiController {
24
25
	private $userId;
26
	private $db;
27
28 View Code Duplication
	public function __construct($appName, IRequest $request, $userId, IDb $db) {
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...
29
		parent::__construct($appName, $request);
30
		$this->userId = $userId;
31
		$this->db = $db;
32
		$this->request = $request;
33
	}
34
35
	/**
36
	 * @NoAdminRequired
37
	 */
38
	public function legacyGetBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") {
39
		return $this->getBookmarks($type, $tag, $page, $sort);
40
	}
41
42
	/**
43
	 * @NoAdminRequired
44
	 */
45
	public function getBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") {
46
47
		if ($type == 'rel_tags') {
48
			$tags = Bookmarks::analyzeTagRequest($tag);
49
			$qtags = Bookmarks::findTags($this->userId, $this->db, $tags);
50
			return new JSONResponse(array('data' => $qtags, 'status' => 'success'));
51
		} else { // type == bookmark
52
			$filterTag = Bookmarks::analyzeTagRequest($tag);
53
54
			$offset = $page * 10;
55
56
			if ($sort == 'bookmarks_sorting_clicks') {
57
				$sqlSortColumn = 'clickcount';
58
			} else {
59
				$sqlSortColumn = 'lastmodified';
60
			}
61
			$bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, $offset, $sqlSortColumn, $filterTag, true);
62
			return new JSONResponse(array('data' => $bookmarks, 'status' => 'success'));
63
		}
64
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 */
69
	public function newBookmark($url = "", $item = array(), $from_own = 0, $title = "", $is_public = false, $description = "") {
70
71
		// Check if it is a valid URL
72
		$urlData = parse_url($url);
73 View Code Duplication
		if ($urlData === false || !isset($urlData['scheme']) || !isset($urlData['host'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
			return new JSONResponse(array('status' => 'error'), Http::STATUS_BAD_REQUEST);
75
		}
76
77
		$tags = isset($item['tags']) ? $item['tags'] : array();
78
79
		if ($from_own == 0) {
80
			$datas = Bookmarks::getURLMetadata($url);
81
			if (isset($datas['title'])) {
82
				$title = $datas['title'];
83
			}
84
		}
85
		$id = Bookmarks::addBookmark($this->userId, $this->db, $url, $title, $tags, $description, $is_public);
86
		$bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db);
87
		return new JSONResponse(array('item' => $bm, 'status' => 'success'));
88
	}
89
90
	/**
91
	  @NoAdminRequired
92
	 * 
93
	 * @param int $id
94
	 * @param bool $is_public Description
95
	 * @return \OCP\AppFramework\Http\TemplateResponse
96
	 */
97
	//TODO id vs record_id?
98
	public function legacyEditBookmark($id = null, $url = "", $item = array(), $title = "", $is_public = false, $record_id = null, $description = "") {
99
		if ($id == null) {
100
			return $this->newBookmark($url, $item, false, $title, $is_public, $description);
101
		} else {
102
			return $this->editBookmark($id, $url, $item, $title, $is_public, $record_id, $description);
103
		}
104
	}
105
106
	/**
107
	  @NoAdminRequired
108
	 * 
109
	 * @param int $id
110
	 * @param bool $is_public Description
111
	 * @return \OCP\AppFramework\Http\TemplateResponse
112
	 */
113
	public function editBookmark($id = null, $url = "", $item = array(), $title = "", $is_public = false, $record_id = null, $description = "") {
114
115
		// Check if it is a valid URL
116
		$urlData = parse_url($url);
117 View Code Duplication
		if ($urlData === false || !isset($urlData['scheme']) || !isset($urlData['host'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
118
			return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
119
		}
120
121
		if ($record_id == null) {
122
			return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
123
		}
124
125
		$tags = isset($item['tags']) ? $item['tags'] : array();
126
127
		if (is_numeric($record_id)) {
128
			$id = Bookmarks::editBookmark($this->userId, $this->db, $record_id, $url, $title, $tags, $description, $is_public = false);
129
		}
130
131
		$bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db);
132
		return new JSONResponse(array('item' => $bm, 'status' => 'success'));
133
	}
134
135
	/**
136
	  @NoAdminRequired
137
	 * 
138
	 * @param int $id
139
	 * @param bool $is_public Description
0 ignored issues
show
Bug introduced by
There is no parameter named $is_public. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
140
	 * @return \OCP\AppFramework\Http\JSONResponse
141
	 */
142
	public function legacyDeleteBookmark($id = -1) {
143
		return $this->deleteBookmark($id);
144
	}
145
146
	/**
147
	  @NoAdminRequired
148
	 * 
149
	 * @param int $id
150
	 * @param bool $is_public Description
0 ignored issues
show
Bug introduced by
There is no parameter named $is_public. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
151
	 * @return \OCP\AppFramework\Http\JSONResponse
152
	 */
153
	public function deleteBookmark($id = -1) {
154
		if ($id == -1) {
155
			return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
156
		}
157
158
		if (!Bookmarks::deleteUrl($this->userId, $this->db, $id)) {
159
			return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
160
		} else {
161
			return new JSONResponse(array('status' => 'success'), Http::STATUS_OK);
162
		}
163
	}
164
165
	/**
166
	  @NoAdminRequired
167
	 * 
168
	 * @param string $url
169
	 * @return \OCP\AppFramework\Http\JSONResponse
170
	 */
171
	public function clickBookmark($url = "") {
172
173
		// Check if it is a valid URL
174
		$urlData = parse_url($url);
175 View Code Duplication
		if ($urlData === false || !isset($urlData['scheme']) || !isset($urlData['host'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
176
			return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
177
		}
178
179
		$query = $this->db->prepareQuery('
180
	UPDATE `*PREFIX*bookmarks`
181
	SET `clickcount` = `clickcount` + 1
182
	WHERE `user_id` = ?
183
		AND `url` LIKE ?
184
	');
185
186
		$params = array($this->userId, htmlspecialchars_decode($url));
187
		$query->execute($params);
188
189
		return new JSONResponse(array('status' => 'success'), Http::STATUS_OK);
190
	}
191
192
	/**
193
	  @NoAdminRequired
194
	 * 
195
	 * @return \OCP\AppFramework\Http\JSONResponse
196
	 */
197
	public function importBookmark() {
198
199
		$l = new \OC_l10n('bookmarks');
200
201
		$full_input = $this->request->getUploadedFile("bm_import");
202
203
		if (empty($full_input)) {
204
			\OCP\Util::writeLog('bookmarks', "No file provided for import", \OCP\Util::WARN);
205
			$error = array();
206
			$error[] = $l->t('No file provided for import');
207
		} else {
208
			$error = array();
209
			$file = $full_input['tmp_name'];
210
			if ($full_input['type'] == 'text/html') {
211
				$error = Bookmarks::importFile($this->userId, $this->db, $file);
212
				if (empty($error)) {
213
					return new JSONResponse(array('status' => 'success'));
214
				}
215
			} else {
216
				$error[] = $l->t('Unsupported file type for import');
217
			}
218
		}
219
220
		return new JSONResponse(array('status' => 'error', 'data' => $error));
221
	}
222
223
	/**
224
	  @NoAdminRequired
225
	 * 
226
	 * @return \OCP\AppFramework\Http\JSONResponse
227
	 */
228
	public function exportBookmark() {
229
230
		$file = <<<EOT
231
<!DOCTYPE NETSCAPE-Bookmark-file-1>
232
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
233
<!-- This is an automatically generated file.
234
It will be read and overwritten.
235
Do Not Edit! -->
236
<TITLE>Bookmarks</TITLE>
237
<H1>Bookmarks</H1>
238
<DL><p>
239
EOT;
240
		$bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, 0, 'id', array(), true, -1);
241
		foreach ($bookmarks as $bm) {
242
			$title = $bm['title'];
243
			if (trim($title) === '') {
244
				$url_parts = parse_url($bm['url']);
245
				$title = isset($url_parts['host']) ? OCA\Bookmarks\Controller\Lib\Helper::getDomainWithoutExt($url_parts['host']) : $bm['url'];
246
			}
247
			$file .= '<DT><A HREF="' . \OC_Util::sanitizeHTML($bm['url']) . '" TAGS="' . implode(',', \OC_Util::sanitizeHTML($bm['tags'])) . '">';
248
			$file .= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</A>';
249
			if ($bm['description'])
250
				$file .= '<DD>' . htmlspecialchars($bm['description'], ENT_QUOTES, 'UTF-8');
251
			$file .= "\n";
252
		}
253
254
		return new ExportResponse($file);
255
	}
256
257
}
258