Completed
Push — master ( 58b59e...315c0b )
by René
03:48 queued 03:38
created

Poll::getDescriptionSafe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[email protected]>
6
 * @author Kai Schröer <[email protected]>
7
 * @author René Gieling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 *  This program is free software: you can redistribute it and/or modify
12
 *  it under the terms of the GNU Affero General Public License as
13
 *  published by the Free Software Foundation, either version 3 of the
14
 *  License, or (at your option) any later version.
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
22
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\Polls\Db;
27
28
use JsonSerializable;
29
30
use OCP\IUser;
31
use OCP\AppFramework\Db\Entity;
32
use OCA\Polls\Model\User;
33
34
/**
35
 * @method string getType()
36
 * @method void setType(string $value)
37
 * @method string getTitle()
38
 * @method void setTitle(string $value)
39
 * @method string getDescription()
40
 * @method void setDescription(string $value)
41
 * @method string getOwner()
42
 * @method void setOwner(string $value)
43
 * @method int getCreated()
44
 * @method void setCreated(integer $value)
45
 * @method int getExpire()
46
 * @method void setExpire(integer $value)
47
 * @method int getDeleted()
48
 * @method void setDeleted(integer $value)
49
 * @method string getAccess()
50
 * @method void setAccess(string $value)
51
 * @method int getAnonymous()
52
 * @method void setAnonymous(integer $value)
53
 * @method int getFullAnonymous()
54
 * @method void setFullAnonymous(integer $value)
55
 * @method int getallowComment()
56
 * @method void setallowComment(integer $value)
57
 * @method int getAllowMaybe()
58
 * @method void setAllowMaybe(integer $value)
59
 * @method string getOptions()
60
 * @method void setOptions(string $value)
61
 * @method string getSettings()
62
 * @method void setSettings(string $value)
63
 * @method int getVoteLimit()
64
 * @method void setVoteLimit(integer $value)
65
 * @method int getOptionLimit()
66
 * @method void setOptionLimit(integer $value)
67
 * @method string getShowResults()
68
 * @method void setShowResults(string $value)
69
 * @method int getAdminAccess()
70
 * @method void setAdminAccess(integer $value)
71
 * @method int getImportant()
72
 * @method void setImportant(integer $value)
73
 * @method int getHideBookedUp()
74
 * @method void setHideBookedUp(integer $value)
75
 */
76
class Poll extends Entity implements JsonSerializable {
77
	public const TYPE_DATE = 'datePoll';
78
	public const TYPE_TEXT = 'textPoll';
79
	public const ACCESS_HIDDEN = 'hidden';
80
	public const ACCESS_PUBLIC = 'public';
81
	public const SHOW_RESULTS_ALWAYS = 'always';
82
	public const SHOW_RESULTS_CLOSED = 'closed';
83
	public const SHOW_RESULTS_NEVER = 'never';
84
85
	/** @var string $type */
86
	protected $type;
87
88
	/** @var string $title */
89
	protected $title;
90
91
	/** @var string $description */
92
	protected $description;
93
94
	/** @var string $owner */
95
	protected $owner;
96
97
	/** @var int $created */
98
	protected $created;
99
100
	/** @var int $expire */
101
	protected $expire;
102
103
	/** @var int $deleted */
104
	protected $deleted;
105
106
	/** @var string $access */
107
	protected $access;
108
109
	/** @var int $anonymous */
110
	protected $anonymous;
111
112
	/** @var int $fullAnonymous */
113
	protected $fullAnonymous;
114
115
	/** @var int $allowMaybe */
116
	protected $allowMaybe;
117
118
	/** @var string $options */
119
	protected $options;
120
121
	/** @var string $settings*/
122
	protected $settings;
123
124
	/** @var int $voteLimit*/
125
	protected $voteLimit;
126
127
	/** @var int $optionLimit*/
128
	protected $optionLimit;
129
130
	/** @var string $showResults */
131
	protected $showResults;
132
133
	/** @var int $adminAccess*/
134
	protected $adminAccess;
135
136
	/** @var int $important*/
137
	protected $important;
138
139
	/** @var int $allowComment*/
140
	protected $allowComment;
141
142
	/** @var int $hideBookedUp*/
143
	protected $hideBookedUp;
144
145
	public function jsonSerialize() {
146
		return [
147
			'id' => intval($this->id),
148
			'type' => $this->type,
149
			'title' => $this->title,
150
			'description' => $this->description,
151
			'descriptionSafe' => $this->getDescriptionSafe(),
152
			'owner' => $this->owner,
153
			'created' => intval($this->created),
154
			'expire' => intval($this->expire),
155
			'deleted' => intval($this->deleted),
156
			'access' => $this->access,
157
			'anonymous' => intval($this->anonymous),
158
			'allowComment' => intval($this->allowComment),
159
			'allowMaybe' => intval($this->allowMaybe),
160
			'settings' => $this->settings,
161
			'voteLimit' => intval($this->voteLimit),
162
			'optionLimit' => intval($this->optionLimit),
163
			'showResults' => $this->showResults === 'expired' ? Poll::SHOW_RESULTS_CLOSED : $this->showResults,
164
			'adminAccess' => intVal($this->adminAccess),
165
			'ownerDisplayName' => $this->getDisplayName(),
166
			'important' => intVal($this->important),
167
			'hideBookedUp' => intVal($this->hideBookedUp)
168
		];
169
	}
170
171
	/**
172
	 * @return static
173
	 */
174
	public function deserializeArray(array $array): self {
175
		$this->setTitle($array['title'] ?? $this->getTitle());
176
		$this->setDescription($array['description'] ?? $this->getDescription());
177
		$this->setAccess($array['access'] ?? $this->getAccess());
178
		$this->setExpire($array['expire'] ?? $this->getExpire());
179
		$this->setAnonymous($array['anonymous'] ?? $this->getAnonymous());
180
		$this->setallowComment($array['allowComment'] ?? $this->getallowComment());
181
		$this->setAllowMaybe($array['allowMaybe'] ?? $this->getAllowMaybe());
182
		$this->setVoteLimit($array['voteLimit'] ?? $this->getVoteLimit());
183
		$this->setOptionLimit($array['optionLimit'] ?? $this->getOptionLimit());
184
		$this->setShowResults($array['showResults'] ?? $this->getShowResults());
185
		$this->setDeleted($array['deleted'] ?? $this->getDeleted());
186
		$this->setAdminAccess($array['adminAccess'] ?? $this->getAdminAccess());
187
		$this->setImportant($array['important'] ?? $this->getImportant());
188
		$this->setHideBookedUp($array['hideBookedUp'] ?? $this->getHideBookedUp());
189
		return $this;
190
	}
191
192
	public function getExpired(): bool {
193
		return (
194
			   $this->getExpire() > 0
195
			&& $this->getExpire() < time()
196
		);
197
	}
198
199
	public function getDescriptionSafe() {
200
		return htmlspecialchars($this->description);
201
	}
202
203
	private function getDisplayName(): string {
204
		return \OC::$server->getUserManager()->get($this->owner) instanceof IUser
205
			? \OC::$server->getUserManager()->get($this->owner)->getDisplayName()
206
			: $this->owner;
207
	}
208
209
	public function getOwnerUserObject(): User {
210
		return new User($this->owner);
211
	}
212
}
213