Passed
Pull Request — master (#1038)
by René
04:16
created

OptionController::findCalendarEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Controller;
25
26
use DateTime;
27
use DateInterval;
28
use OCA\Polls\Exceptions\DuplicateEntryException;
29
30
use OCP\IRequest;
31
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCA\Polls\Service\OptionService;
36
use OCA\Polls\Service\CalendarService;
37
38
class OptionController extends Controller {
39
40
	/** @var OptionService */
41
	private $optionService;
42
43
	/** @var CalendarService */
44
	private $calendarService;
45
46
	/**
47
	 * OptionController constructor.
48
	 * @param string $appName
49
	 * @param IRequest $request
50
	 * @param OptionService $optionService
51
	 */
52
53
	public function __construct(
54
		string $appName,
55
		IRequest $request,
56
		OptionService $optionService,
57
		CalendarService $calendarService
58
	) {
59
		parent::__construct($appName, $request);
60
		$this->optionService = $optionService;
61
		$this->calendarService = $calendarService;
62
	}
63
64
	/**
65
	 * Get all options of given poll
66
	 * @NoAdminRequired
67
	 * @param int $pollId
68
	 * @return DataResponse
69
	 */
70
	public function list($pollId) {
71
		return new DataResponse(['options' => $this->optionService->list($pollId)], Http::STATUS_OK);
72
	}
73
74
	/**
75
	 * Add a new option
76
	 * @NoAdminRequired
77
	 * @param array $option
78
	 * @return DataResponse
79
	 */
80
	public function add($pollId, $timestamp = 0, $pollOptionText = '') {
81
		try {
82
			return new DataResponse(['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText)], Http::STATUS_OK);
83
		} catch (DuplicateEntryException $e) {
84
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
85
		}
86
	}
87
88
	/**
89
	 * Update option
90
	 * @NoAdminRequired
91
	 * @param array $option
92
	 * @return DataResponse
93
	 */
94
	public function update($optionId, $timestamp, $pollOptionText) {
95
		return new DataResponse(['option' => $this->optionService->update($optionId, $timestamp, $pollOptionText)], Http::STATUS_OK);
96
	}
97
98
	/**
99
	 * Delete option
100
	 * @NoAdminRequired
101
	 * @param Option $option
102
	 * @return DataResponse
103
	 */
104
	public function delete($optionId) {
105
		return new DataResponse(['option' => $this->optionService->delete($optionId)], Http::STATUS_OK);
106
	}
107
108
	/**
109
	 * Switch option confirmation
110
	 * @NoAdminRequired
111
	 * @param int $optionId
112
	 * @return DataResponse
113
	 */
114
	public function confirm($optionId) {
115
		return new DataResponse(['option' => $this->optionService->confirm($optionId)], Http::STATUS_OK);
116
	}
117
118
	/**
119
	 * Reorder options
120
	 * @NoAdminRequired
121
	 * @param int $pollId
122
	 * @param Array $options
123
	 * @return DataResponse
124
	 */
125
	public function reorder($pollId, $options) {
126
		return new DataResponse(['options' => $this->optionService->reorder($pollId, $options)], Http::STATUS_OK);
127
	}
128
129
	/**
130
	 * Reorder options
131
	 * @NoAdminRequired
132
	 * @param int $optionId
133
	 * @param int $step
134
	 * @param string $unit
135
	 * @param int $amount
136
	 * @return DataResponse
137
	 */
138
	public function sequence($optionId, $step, $unit, $amount) {
139
		return new DataResponse(['options' => $this->optionService->sequence($optionId, $step, $unit, $amount)], Http::STATUS_OK);
140
	}
141
142
	/**
143
	 * findCalendarEvents
144
	 * @NoAdminRequired
145
	 * @param integer $from
146
	 * @param integer $to
147
	 * @return DataResponse
148
	 */
149
	public function findCalendarEvents($optionId) {
150
		$searchFrom = new DateTime();
151
		$searchFrom = $searchFrom->setTimestamp($this->optionService->get($optionId)->getTimestamp())->sub(new DateInterval('PT1H'));
152
		$searchTo = clone $searchFrom;
153
		$searchTo = $searchTo->add(new DateInterval('PT3H'));
154
155
		return new DataResponse(['events' => array_values($this->calendarService->getEvents($searchFrom, $searchTo))], Http::STATUS_OK);
156
	}
157
}
158