Passed
Pull Request — master (#1254)
by René
03:50
created

Poll::getExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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