Completed
Push — master ( 3a7c63...41526f )
by René
01:23 queued 01:19
created

OptionController::shift()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 OCP\IRequest;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCA\Polls\Service\OptionService;
32
use OCA\Polls\Service\CalendarService;
33
34
class OptionController extends Controller {
35
36
	/** @var OptionService */
37
	private $optionService;
38
39
	/** @var CalendarService */
40
	private $calendarService;
41
42
	use ResponseHandle;
43
44
	public function __construct(
45
		string $appName,
46
		IRequest $request,
47
		OptionService $optionService,
48
		CalendarService $calendarService
49
	) {
50
		parent::__construct($appName, $request);
51
		$this->optionService = $optionService;
52
		$this->calendarService = $calendarService;
53
	}
54
55
	/**
56
	 * Get all options of given poll
57
	 * @NoAdminRequired
58
	 */
59
	public function list($pollId): DataResponse {
60
		return $this->response(function () use ($pollId) {
61
			return ['options' => $this->optionService->list($pollId)];
62
		});
63
	}
64
65
	/**
66
	 * Add a new option
67
	 * @NoAdminRequired
68
	 */
69
	public function add($pollId, $timestamp = 0, $pollOptionText = '', $duration = 0): DataResponse {
70
		return $this->responseCreate(function () use ($pollId, $timestamp, $pollOptionText, $duration) {
71
			return ['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText, $duration)];
72
		});
73
	}
74
75
	/**
76
	 * Update option
77
	 * @NoAdminRequired
78
	 */
79
	public function update($optionId, $timestamp, $pollOptionText, $duration): DataResponse {
80
		return $this->response(function () use ($optionId, $timestamp, $pollOptionText, $duration) {
81
			return ['option' => $this->optionService->update($optionId, $timestamp, $pollOptionText, $duration)];
82
		});
83
	}
84
85
	/**
86
	 * Delete option
87
	 * @NoAdminRequired
88
	 */
89
	public function delete($optionId): DataResponse {
90
		return $this->responseDeleteTolerant(function () use ($optionId) {
91
			return ['option' => $this->optionService->delete($optionId)];
92
		});
93
	}
94
95
	/**
96
	 * Switch option confirmation
97
	 * @NoAdminRequired
98
	 */
99
	public function confirm($optionId): DataResponse {
100
		return $this->response(function () use ($optionId) {
101
			return ['option' => $this->optionService->confirm($optionId)];
102
		});
103
	}
104
105
	/**
106
	 * Reorder options
107
	 * @NoAdminRequired
108
	 */
109
	public function reorder($pollId, $options): DataResponse {
110
		return $this->response(function () use ($pollId, $options) {
111
			return ['options' => $this->optionService->reorder($pollId, $options)];
112
		});
113
	}
114
115
	/**
116
	 * Reorder options
117
	 * @NoAdminRequired
118
	 */
119
	public function sequence($optionId, $step, $unit, $amount): DataResponse {
120
		return $this->response(function () use ($optionId, $step, $unit, $amount) {
121
			return ['options' => $this->optionService->sequence($optionId, $step, $unit, $amount)];
122
		});
123
	}
124
125
	/**
126
	 * Reorder options
127
	 * @NoAdminRequired
128
	 */
129
	public function shift($pollId, $step, $unit): DataResponse {
130
		return $this->response(function () use ($pollId, $step, $unit) {
131
			return ['options' => $this->optionService->shift($pollId, $step, $unit)];
132
		});
133
	}
134
135
	/**
136
	 * findCalendarEvents
137
	 * @NoAdminRequired
138
	 */
139
	public function findCalendarEvents($optionId): DataResponse {
140
		return $this->response(function () use ($optionId) {
141
			$option = $this->optionService->get($optionId);
142
			$searchFrom = new DateTime();
143
			$searchTo = new DateTime();
144
			// Search calendar entries which end inside one hour before option start time
145
			$searchFrom = $searchFrom->setTimestamp($option->getTimestamp())->sub(new DateInterval('PT1H'));
146
			// Search calendar entries which start inside one hour after option end time
147
			$searchTo = $searchTo->setTimestamp($option->getTimestamp() + $option->getDuration())->add(new DateInterval('PT1H'));
148
			$events = $this->calendarService->getEvents($searchFrom, $searchTo);
149
			return ['events' => $events];
150
		});
151
	}
152
}
153