Passed
Push — master ( 6d21e0...1f4c02 )
by Morris
14:24 queued 10s
created

WeatherStatusController::usePersonalAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Julien Veyssier
7
 *
8
 * @author Julien Veyssier <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OCA\WeatherStatus\Controller;
26
27
use OCA\WeatherStatus\Service\WeatherStatusService;
28
use OCP\AppFramework\Http;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCSController;
31
use OCP\ILogger;
32
use OCP\IRequest;
33
34
class WeatherStatusController extends OCSController {
35
36
	/** @var string */
37
	private $userId;
38
39
	/** @var ILogger */
40
	private $logger;
41
42
	/** @var WeatherStatusService */
43
	private $service;
44
45
	public function __construct(string $appName,
46
								IRequest $request,
47
								ILogger $logger,
48
								WeatherStatusService $service,
49
								string $userId) {
50
		parent::__construct($appName, $request);
51
		$this->userId = $userId;
52
		$this->logger = $logger;
53
		$this->service = $service;
54
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 *
59
	 * Try to use the address set in user personal settings as weather location
60
	 *
61
	 * @return DataResponse with success state and address information
62
	 */
63
	public function usePersonalAddress(): DataResponse {
64
		return new DataResponse($this->service->usePersonalAddress());
65
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 *
70
	 * Change the weather status mode. There are currently 2 modes:
71
	 * - ask the browser
72
	 * - use the user defined address
73
	 *
74
	 * @param int $mode New mode
75
	 * @return DataResponse success state
76
	 */
77
	public function setMode(int $mode): DataResponse {
78
		return new DataResponse($this->service->setMode($mode));
79
	}
80
81
	/**
82
	 * @NoAdminRequired
83
	 *
84
	 * Set address and resolve it to get coordinates
85
	 * or directly set coordinates and get address with reverse geocoding
86
	 *
87
	 * @param string|null $address Any approximative or exact address
88
	 * @param float|null $lat Latitude in decimal degree format
89
	 * @param float|null $lon Longitude in decimal degree format
90
	 * @return DataResponse with success state and address information
91
	 */
92
	public function setLocation(?string $address, ?float $lat, ?float $lon): DataResponse {
93
		$currentWeather = $this->service->setLocation($address, $lat, $lon);
94
		return new DataResponse($currentWeather);
95
	}
96
97
	/**
98
	 * @NoAdminRequired
99
	 *
100
	 * Get stored user location
101
	 *
102
	 * @return DataResponse which contains coordinates, formatted address and current weather status mode
103
	 */
104
	public function getLocation(): DataResponse {
105
		$location = $this->service->getLocation();
106
		return new DataResponse($location);
107
	}
108
109
	/**
110
	 * @NoAdminRequired
111
	 *
112
	 * Get forecast for current location
113
	 *
114
	 * @return DataResponse which contains success state and filtered forecast data
115
	 */
116
	public function getForecast(): DataResponse {
117
		$forecast = $this->service->getForecast();
118
		if (isset($forecast['success']) && $forecast['success'] === false) {
119
			return new DataResponse($forecast, Http::STATUS_NOT_FOUND);
120
		} else {
121
			return new DataResponse($forecast);
122
		}
123
	}
124
}
125