Completed
Push — master ( f5eb34...6092af )
by
unknown
07:06
created

FavoriteController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 7
c 4
b 0
f 3
lcom 1
cbo 2
dl 0
loc 83
ccs 0
cts 45
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A updateFavorite() 0 17 2
A addFavorite() 0 14 1
A getFavorites() 0 4 1
A removeFavorite() 0 7 2
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
	public function updateFavorite($name, $id) {
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 $id int
93
	 * @return JSONResponse
94
	 */
95
	public function removeFavorite($id){
96
		$favorite = $this->favoriteMapper->find($id);
97
		if($favorite->userId == $this->userId) {
98
			$this->favoriteMapper->delete($favorite);
99
		}
100
		return new JSONResponse();
101
	}
102
103
}
104