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

Option::getOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
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\AppFramework\Db\Entity;
31
use OCP\IUser;
32
33
/**
34
 * @method int getId()
35
 * @method void setId(integer $value)
36
 * @method int getConfirmed()
37
 * @method void setConfirmed(integer $value)
38
 * @method int getDuration()
39
 * @method void setDuration(integer $value)
40
 * @method int getOrder()
41
 * @method void setOrder(integer $value)
42
 * @method string getOwner()
43
 * @method void setOwner(string $value)
44
 * @method int getPollId()
45
 * @method void setPollId(integer $value)
46
 * @method string getPollOptionText()
47
 * @method void setPollOptionText(string $value)
48
 * @method int getReleased()
49
 * @method void setReleased(int $value)
50
 * @method int getTimestamp()
51
 * @method void setTimestamp(integer $value)
52
 */
53
class Option extends Entity implements JsonSerializable {
54
55
	/** @var int $pollId */
56
	protected $pollId;
57
58
	/** @var string $owner */
59
	protected $owner;
60
61
	/** @var int $released */
62
	protected $released;
63
64
	/** @var string $pollOptionText */
65
	protected $pollOptionText;
66
67
	/** @var int $timestamp */
68
	protected $timestamp;
69
70
	/** @var int $order */
71
	protected $order;
72
73
	/** @var int $confirmed */
74
	protected $confirmed;
75
76
	/** @var int $duration */
77
	protected $duration;
78
79
	// public variables, not in the db
80
	/** @var int $rank */
81
	public $rank = 0;
82
83
	/** @var int $yes */
84
	public $yes = 0;
85
86
	/** @var int $no */
87
	public $no = 0;
88
89
	/** @var int $maybe */
90
	public $maybe = 0;
91
92
	/** @var int $realNo */
93
	public $realNo = 0;
94
95
	/** @var int $votes */
96
	public $votes = 0;
97
98
	/** @var bool $isBookedUp */
99
	public $isBookedUp = false;
100
101
	public function __construct() {
102
		$this->addType('released', 'int');
103
		$this->addType('pollId', 'int');
104
		$this->addType('timestamp', 'int');
105
		$this->addType('order', 'int');
106
		$this->addType('confirmed', 'int');
107
		$this->addType('duration', 'int');
108
	}
109
110
	public function jsonSerialize() {
111
		return [
112
			'id' => $this->getId(),
113
			'pollId' => $this->getPollId(),
114
			'owner' => $this->getOwner(),
115
			'ownerDisplayName' => $this->getDisplayName(),
116
			'ownerIsNoUser' => $this->getOwnerIsNoUser(),
117
			'released' => $this->getReleased(),
118
			'pollOptionText' => $this->getPollOptionText(),
119
			'timestamp' => $this->getTimestamp(),
120
			'order' => $this->getOrder(),
121
			'confirmed' => $this->getConfirmed(),
122
			'duration' => $this->getDuration(),
123
			'rank' => $this->rank,
124
			'no' => $this->no,
125
			'yes' => $this->yes,
126
			'maybe' => $this->maybe,
127
			'realNo' => $this->realNo,
128
			'votes' => $this->votes,
129
			'isBookedUp' => $this->isBookedUp,
130
		];
131
	}
132
133
	public function getPollOptionText(): string {
134
		if ($this->timestamp && $this->duration) {
135
			return date('c', $this->timestamp) . ' - ' . date('c', $this->timestamp + $this->duration);
136
		} elseif ($this->timestamp && !$this->duration) {
137
			return date('c', $this->timestamp);
138
		}
139
		return htmlspecialchars_decode($this->pollOptionText);
140
	}
141
142
	public function getOrder(): int {
143
		if ($this->timestamp) {
144
			return $this->timestamp;
145
		}
146
		return $this->order;
147
	}
148
149
	private function getDisplayName(): ?string {
150
		if (!strncmp($this->owner, 'deleted_', 8)) {
151
			return 'Deleted User';
152
		}
153
		return $this->getOwnerIsNoUser()
154
			? $this->owner
155
			: \OC::$server->getUserManager()->get($this->owner)->getDisplayName();
156
	}
157
158
	private function getOwnerIsNoUser(): bool {
159
		return !\OC::$server->getUserManager()->get($this->owner) instanceof IUser;
160
	}
161
}
162