FavoriteController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 17
loc 95
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A updateFavorite() 17 17 2
A addFavorite() 0 14 1
A getFavorites() 0 4 1
A getFavoritesByName() 0 4 1
A removeFavorite() 0 7 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
 * ownCloud - maps
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Vinzenz Rosenkranz <[email protected]>
9
 * @copyright Vinzenz Rosenkranz 2015
10
 */
11
12
namespace OCA\Maps\Controller;
13
14
use OCA\Maps\Db\Favorite;
15
use OCA\Maps\Db\FavoriteMapper;
16
use \OCP\IRequest;
17
use \OCP\AppFramework\Http\JSONResponse;
18
use \OCP\AppFramework\ApiController;
19
20
21
class FavoriteController extends ApiController {
22
23
	private $userId;
24
	private $favoriteMapper;
25
26
	public function __construct($appName, IRequest $request, FavoriteMapper $favoriteMapper, $userId) {
27
		parent::__construct($appName, $request);
28
		$this->favoriteMapper = $favoriteMapper;
29
		$this->userId = $userId;
30
	}
31
32
	/**
33
	 * @NoAdminRequired
34
	 *
35
	 * @param $name string
36
	 * @param $id int
37
	 * @return JSONResponse
38
	 */
39 View Code Duplication
	public function updateFavorite($name, $id) {
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...
40
41
		$favorite = new Favorite();
42
		$favorite->setId($id);
43
		$favorite->setName($name);
44
		$favorite->setTimestamp(time());
45
46
		/* Only save favorite if it exists in db */
47
		try {
48
			$this->favoriteMapper->find($id);
49
			return new JSONResponse($this->favoriteMapper->update($favorite));
50
		} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
51
			return new JSONResponse([
52
				'error' => $e->getMessage()
53
			]);
54
		}
55
	}
56
57
	/**
58
	 * @NoAdminRequired
59
	 *
60
	 * @param $lat float
61
	 * @param $lng float
62
	 * @return JSONResponse
63
	 */
64
	public function addFavorite($lat, $lng, $name = null){
65
		$favorite = new Favorite();
66
		$favorite->setName($name);
67
		$favorite->setTimestamp(time());
68
		$favorite->setUserId($this->userId);
69
		$favorite->setLat($lat);
70
		$favorite->setLng($lng);
71
72
		/* @var $favorite Favorite */
73
		$favorite = $this->favoriteMapper->insert($favorite);
74
75
		$response = array('id'=> $favorite->getId());
76
		return new JSONResponse($response);
77
	}
78
79
	/**
80
	 * @NoAdminRequired
81
	 *
82
	 * @return JSONResponse
83
	 */
84
	public function getFavorites(){
85
		$favorites = $this->favoriteMapper->findAll();
86
		return new JSONResponse($favorites);
87
	}
88
89
	/**
90
	 * @NoAdminRequired
91
	 *
92
	 * @param $name string
93
	 *
94
	 * @return JSONResponse
95
	 */
96
	public function getFavoritesByName($name){
97
		$favorites = $this->favoriteMapper->findByName($name);
98
		return new JSONResponse($favorites);
99
	}
100
101
	/**
102
	 * @NoAdminRequired
103
	 *
104
	 * @param $id int
105
	 * @return JSONResponse
106
	 */
107
	public function removeFavorite($id){
108
		$favorite = $this->favoriteMapper->find($id);
109
		if($favorite->userId == $this->userId) {
110
			$this->favoriteMapper->delete($favorite);
111
		}
112
		return new JSONResponse();
113
	}
114
115
}
116