Passed
Pull Request — master (#966)
by René
04:27
created

OptionService::clone()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 12
rs 10
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\Service;
25
26
use Exception;
27
28
use OCA\Polls\Exceptions\NotAuthorizedException;
29
use OCA\Polls\Db\Option;
30
use OCA\Polls\Db\OptionMapper;
31
use OCA\Polls\Service\LogService;
32
use OCA\Polls\Model\Acl;
33
34
class OptionService  {
35
36
	private $optionMapper;
37
	private $option;
38
	private $logService;
39
	private $acl;
40
41
	/**
42
	 * OptionController constructor.
43
	 * @param OptionMapper $optionMapper
44
	 * @param Option $option
45
	 * @param LogService $logService
46
	 * @param Acl $acl
47
	 */
48
49
	public function __construct(
50
		OptionMapper $optionMapper,
51
		Option $option,
52
		LogService $logService,
53
		Acl $acl
54
	) {
55
		$this->optionMapper = $optionMapper;
56
		$this->option = $option;
57
		$this->logService = $logService;
58
		$this->acl = $acl;
59
	}
60
61
	/**
62
	 * Set properties from option array
63
	 * @NoAdminRequired
64
	 * @param Array $option
65
	 */
66
	private function set($option) {
67
68
		$this->option->setPollId($option['pollId']);
69
		$this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText'])));
70
		$this->option->setTimestamp($option['timestamp']);
71
72
		if ($option['timestamp']) {
73
			$this->option->setOrder($option['timestamp']);
74
		} else {
75
			$this->option->setOrder($option['order']);
76
		}
77
78
		if ($option['confirmed']) {
79
			// do not update confirmation date, if option is already confirmed
80
			if (!$this->option->getConfirmed()) {
81
				$this->option->setConfirmed(time());
82
			}
83
		} else {
84
			$this->option->setConfirmed(0);
85
		}
86
	}
87
88
	/**
89
	 * Get all options of given poll
90
	 * @NoAdminRequired
91
	 * @param integer $pollId
92
	 * @param string $token
93
	 * @return array Array of Option objects
94
	 */
95
	public function list($pollId = 0, $token = '') {
96
97
		if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) {
98
			throw new NotAuthorizedException;
99
		}
100
101
		return $this->optionMapper->findByPoll($pollId);
102
103
	}
104
105
106
	/**
107
	 * Add a new Option to poll
108
	 * @NoAdminRequired
109
	 * @param Array $option
110
	 * @return Option
111
	 */
112
	public function add($option) {
113
114
		if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
115
			throw new NotAuthorizedException;
116
		}
117
118
		$this->option = new Option();
119
		$this->set($option);
120
		$this->optionMapper->insert($this->option);
121
		$this->logService->setLog($option['pollId'], 'addOption');
122
123
		return $this->option;
124
	}
125
126
	/**
127
	 * Remove a single option
128
	 * @NoAdminRequired
129
	 * @param Option $option
130
	 * @return array Array of Option objects
131
	 */
132
	public function delete($optionId) {
133
		$this->option = $this->optionMapper->find($optionId);
134
135
		if (!$this->acl->setPollId($this->option->getPollId())->getAllowEdit()) {
136
			throw new NotAuthorizedException;
137
		}
138
139
		$this->optionMapper->delete($this->option);
140
141
		return $this->option;
142
143
	}
144
145
	/**
146
	 * Update poll option
147
	 * @NoAdminRequired
148
	 * @param array $option
149
	 * @return Option
150
	 */
151
	public function update($option) {
152
		if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) {
153
			throw new NotAuthorizedException;
154
		}
155
156
		try {
157
			$this->option = $this->optionMapper->find($option['id']);
158
			$this->set($option);
159
			$this->optionMapper->update($this->option);
160
			$this->logService->setLog($option['pollId'], 'updateOption');
161
162
			return $this->option;
163
		} catch (Exception $e) {
164
			return new DoesNotExistException($e);
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DoesNotExistException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
165
		}
166
167
	}
168
169
	/**
170
	 * Set order by order of the given array
171
	 * @NoAdminRequired
172
	 * @param array $options
173
	 * @return array Array of Option objects
174
	 */
175
	public function reorder($pollId, $options) {
176
177
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
178
			throw new NotAuthorizedException;
179
		}
180
181
		$i = 0;
182
		foreach ($options as $option) {
183
			$this->option = $this->optionMapper->find($option['id']);
184
			if ($pollId === intval($this->option->getPollId())) {
185
				$this->option->setOrder(++$i);
186
				$this->optionMapper->update($this->option);
187
			}
188
		}
189
190
		return $this->optionMapper->findByPoll($pollId);
191
192
	}
193
194
	/**
195
	 * Set order by order of the given array
196
	 * @NoAdminRequired
197
	 * @param integer $fromPollId
198
	 * @param integer $toPollId
199
	 * @return array Array of Option objects
200
	 */
201
	public function clone($fromPollId, $toPollId) {
202
203
		if (!$this->acl->setPollId($fromPollId)->getAllowView()) {
204
			throw new NotAuthorizedException;
205
		}
206
207
		foreach ($this->optionMapper->findByPoll($fromPollId) as $option) {
208
			$option->setPollId($toPollId);
209
			$this->optionMapper->insert($option);
210
		}
211
212
		return $this->optionMapper->findByPoll($toPollId);
213
214
	}
215
}
216