LocationController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 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 Sander Brand <[email protected]>
9
 * @copyright Sander Brand 2014
10
 */
11
12
namespace OCA\Maps\Controller;
13
14
use OCA\Maps\Db\Device;
15
use OCA\Maps\Db\DeviceMapper;
16
use OCA\Maps\Db\Location;
17
use OCA\Maps\Db\LocationMapper;
18
use \OCP\IRequest;
19
use \OCP\AppFramework\Http\JSONResponse;
20
use \OCP\AppFramework\ApiController;
21
22
23
class LocationController extends ApiController {
24
25
	private $userId;
26
	private $locationMapper;
27
	private $deviceMapper;
28
29
	public function __construct($appName, IRequest $request, LocationMapper $locationMapper, DeviceMapper $deviceMapper, $userId) {
30
		parent::__construct($appName, $request);
31
		$this->locationMapper = $locationMapper;
32
		$this->deviceMapper = $deviceMapper;
33
		$this->userId = $userId;
34
	}
35
36
	/**
37
	 * @NoAdminRequired
38
	 *
39
	 * @param $lat int
40
	 * @param $lon int
41
	 * @param $timestamp string
42
	 * @param $hdop string
43
	 * @param $altitude int
44
	 * @param $speed int
45
	 * @param $hash string
46
	 * @return JSONResponse
47
	 */
48
	public function update($lat, $lon, $timestamp, $hdop, $altitude, $speed, $hash) {
49
50
		$location = new Location();
51
		$location->lat = $lat;
52
		$location->lng = $lon;
53
		if((string)(float)$timestamp === $timestamp) {
54
			if(strtotime(date('d-m-Y H:i:s',$timestamp)) === (int)$timestamp) {
55
				$location->timestamp = (int)$timestamp;
56
			} elseif(strtotime(date('d-m-Y H:i:s',$timestamp/1000)) === (int)floor($timestamp/1000)) {
57
				$location->timestamp = (int)floor($timestamp/1000);
58
			}
59
		} else {
60
			$location->timestamp = strtotime($timestamp);
61
		}
62
		$location->hdop = $hdop;
63
		$location->altitude = $altitude;
64
		$location->speed = $speed;
65
		$location->deviceHash = $hash;
66
67
		/* Only save location if hash exists in db */
68
		try {
69
			$this->deviceMapper->findByHash($hash);
70
			return new JSONResponse($this->locationMapper->insert($location));
71
		} 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...
72
			return new JSONResponse([
73
				'error' => $e->getMessage()
74
			]);
75
		}
76
	}
77
78
	/**
79
	 * @NoAdminRequired
80
	 *
81
	 * @param $name string
82
	 * @return JSONResponse
83
	 */
84
	public function addDevice($name){
85
		$device = new Device();
86
		$device->name = $name;
87
		$device->hash = uniqid();
88
		$device->created = time();
89
		$device->userId = $this->userId;
90
91
		/* @var $device Device */
92
		$device = $this->deviceMapper->insert($device);
93
94
		$response = array('id'=> $device->getId(),'hash'=>$device->hash);
95
		return new JSONResponse($response);
96
	}
97
98
	/**
99
	 * @NoAdminRequired
100
	 *
101
	 * @return JSONResponse
102
	 */
103
	public function loadDevices(){
104
		$response = $this->deviceMapper->findAll($this->userId);
105
		return new JSONResponse($response);
106
	}
107
108
	/**
109
	 * @NoAdminRequired
110
	 *
111
	 * @param $devices string comma separated list of device ids
112
	 * @param $from string
113
	 * @param $till string
114
	 * @param $limit int
115
	 * @return JSONResponse
116
	 */
117
	public function loadLocations($devices, $from, $till, $limit){
118
		$deviceIds = explode(',',$devices);
119
		$from = ($from) ? strtotime($from) : null;
120
		$till = ($till !== '') ? strtotime($till) : strtotime('now');
121
		$limit = ($limit !== '') ? (int) $limit : 2000;
122
		$response = array();
123
		foreach($deviceIds as $deviceId){
124
			$hash = $this->deviceMapper->findById($deviceId)->hash;
125
			$response[$deviceId] = $this->locationMapper->findBetween($hash, $from, $till, $limit);
126
		}
127
		return new JSONResponse($response);
128
	}
129
130
	/**
131
	 * @NoAdminRequired
132
	 *
133
	 * @param $deviceId string
134
	 * @return JSONResponse
135
	 */
136
	public function removeDevice($deviceId){
137
		/* @var $device Device */
138
		$device = $this->deviceMapper->findById($deviceId);
139
		if($device->userId == $this->userId) {
140
			$this->deviceMapper->delete($device);
141
		}
142
		return new JSONResponse();
143
	}
144
145
}
146