Passed
Push — master ( 0259d4...eb77b7 )
by René
04:31 queued 10s
created

Comment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
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
33
/**
34
 * @method int getId()
35
 * @method void setId(integer $value)
36
 * @method int getPollId()
37
 * @method void setPollId(integer $value)
38
 * @method string getUserId()
39
 * @method void setUserId(string $value)
40
 * @method string getDt()
41
 * @method void setDt(string $value)
42
 * @method string getComment()
43
 * @method void setComment(string $value)
44
 * @method string getTimestamp()
45
 * @method void setTimestamp(integer $value)
46
 */
47
class Comment extends Entity implements JsonSerializable {
48
49
	/** @var int $pollId */
50
	protected $pollId;
51
52
	/** @var string $userId */
53
	protected $userId;
54
55
	/** @var string $dt */
56
	protected $dt;
57
58
	/** @var int $timestamp */
59
	protected $timestamp;
60
61
	/** @var string $comment */
62
	protected $comment;
63
64
	public function __construct() {
65
		$this->addType('pollId', 'integer');
66
		$this->addType('timestamp', 'integer');
67
	}
68
69
	public function jsonSerialize() {
70
		return [
71
			'id' => $this->id,
72
			'pollId' => $this->pollId,
73
			'userId' => $this->userId,
74
			'dt' => $this->dt,
75
			'timestamp' => $this->getTimestamp(),
76
			'comment' => $this->comment,
77
			'isNoUser' => $this->getIsNoUser(),
78
			'displayName' => $this->getDisplayName()
79
		];
80
	}
81
82
	public function getTimestamp(): int {
83
		// too lazy for a migration
84
		// use timestamp if set,
85
		// otherwise use dt and convert to timestamp
86
		return intval($this->timestamp ? $this->timestamp : strtotime($this->dt));
87
	}
88
89
	public function getDisplayName(): string {
90
		return \OC::$server->getUserManager()->get($this->userId) instanceof IUser
91
			? \OC::$server->getUserManager()->get($this->userId)->getDisplayName()
92
			: $this->userId;
93
	}
94
95
	public function getIsNoUser(): bool {
96
		return !(\OC::$server->getUserManager()->get($this->userId) instanceof IUser);
97
	}
98
}
99