Passed
Push — master ( 60d45f...a37810 )
by René
05:02 queued 01:25
created

Poll::deserializeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 2
cp 0
rs 9.8333
cc 1
nc 1
nop 1
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 getAllowMaybe()
56
 * @method void setAllowMaybe(integer $value)
57
 * @method string getOptions()
58
 * @method void setOptions(string $value)
59
 * @method string getSettings()
60
 * @method void setSettings(string $value)
61
 * @method int getVoteLimit()
62
 * @method void setVoteLimit(integer $value)
63
 * @method int getOptionLimit()
64
 * @method void setOptionLimit(integer $value)
65
 * @method string getShowResults()
66
 * @method void setShowResults(string $value)
67
 * @method int getAdminAccess()
68
 * @method void setAdminAccess(integer $value)
69
 * @method int getImportant()
70
 * @method void setImportant(integer $value)
71
 */
72
class Poll extends Entity implements JsonSerializable {
73
	public const TYPE_DATE = 'datePoll';
74
	public const TYPE_TEXT = 'textPoll';
75
	public const ACCESS_HIDDEN = 'hidden';
76
	public const ACCESS_PUBLIC = 'public';
77
	public const SHOW_RESULTS_ALWAYS = 'always';
78
	public const SHOW_RESULTS_CLOSED = 'closed';
79
	public const SHOW_RESULTS_NEVER = 'never';
80
81
	/** @var string $type */
82
	protected $type;
83
84
	/** @var string $title */
85
	protected $title;
86
87
	/** @var string $description */
88
	protected $description;
89
90
	/** @var string $owner */
91
	protected $owner;
92
93
	/** @var int $created */
94
	protected $created;
95
96
	/** @var int $expire */
97
	protected $expire;
98
99
	/** @var int $deleted */
100
	protected $deleted;
101
102
	/** @var string $access */
103
	protected $access;
104
105
	/** @var int $anonymous */
106
	protected $anonymous;
107
108
	/** @var int $fullAnonymous */
109
	protected $fullAnonymous;
110
111
	/** @var int $allowMaybe */
112
	protected $allowMaybe;
113
114
	/** @var string $options */
115
	protected $options;
116
117
	/** @var string $settings*/
118
	protected $settings;
119
120
	/** @var int $voteLimit*/
121
	protected $voteLimit;
122
123
	/** @var int $optionLimit*/
124
	protected $optionLimit;
125
126
	/** @var string $showResults */
127
	protected $showResults;
128
129
	/** @var int $adminAccess*/
130
	protected $adminAccess;
131
132
	/** @var int $important*/
133
	protected $important;
134
135
	public function jsonSerialize() {
136
		return [
137
			'id' => intval($this->id),
138
			'type' => $this->type,
139
			'title' => $this->title,
140
			'description' => $this->description,
141
			'owner' => $this->owner,
142
			'created' => intval($this->created),
143
			'expire' => intval($this->expire),
144
			'deleted' => intval($this->deleted),
145
			'access' => $this->access,
146
			'anonymous' => intval($this->anonymous),
147
			'allowMaybe' => intval($this->allowMaybe),
148
			'settings' => $this->settings,
149
			'voteLimit' => intval($this->voteLimit),
150
			'optionLimit' => intval($this->optionLimit),
151
			'showResults' => $this->showResults === 'expired' ? Poll::SHOW_RESULTS_CLOSED : $this->showResults,
152
			'adminAccess' => intVal($this->adminAccess),
153
			'ownerDisplayName' => $this->getDisplayName(),
154
			'important' => intVal($this->important)
155
		];
156
	}
157
158
	/**
159
	 * @return static
160
	 */
161
	public function deserializeArray(array $array): self {
162
		$this->setTitle($array['title'] ?? $this->getTitle());
163
		$this->setDescription($array['description'] ?? $this->getDescription());
164
		$this->setAccess($array['access'] ?? $this->getAccess());
165
		$this->setExpire($array['expire'] ?? $this->getExpire());
166
		$this->setAnonymous($array['anonymous'] ?? $this->getAnonymous());
167
		$this->setAllowMaybe($array['allowMaybe'] ?? $this->getAllowMaybe());
168
		$this->setVoteLimit($array['voteLimit'] ?? $this->getVoteLimit());
169
		$this->setOptionLimit($array['optionLimit'] ?? $this->getOptionLimit());
170
		$this->setShowResults($array['showResults'] ?? $this->getShowResults());
171
		$this->setDeleted($array['deleted'] ?? $this->getDeleted());
172
		$this->setAdminAccess($array['adminAccess'] ?? $this->getAdminAccess());
173
		$this->setImportant($array['important'] ?? $this->getImportant());
174
		return $this;
175
	}
176
177
	public function getExpired(): bool {
178
		return (
179
			   $this->getExpire() > 0
180
			&& $this->getExpire() < time()
181
		);
182
	}
183
184
	private function getDisplayName(): string {
185
		return \OC::$server->getUserManager()->get($this->owner) instanceof IUser
186
			? \OC::$server->getUserManager()->get($this->owner)->getDisplayName()
187
			: $this->owner;
188
	}
189
190
	public function getOwnerUserObject(): User {
191
		return new User($this->owner);
192
	}
193
}
194