Share::getMembers()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.9
cc 4
nc 2
nop 0
crap 20
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Db;
25
26
use JsonSerializable;
27
28
use OCP\AppFramework\Db\Entity;
29
use OCA\Polls\Model\UserGroupClass;
30
31
/**
32
 * @method int getId()
33
 * @method void setId(integer $value)
34
 * @method string getToken()
35
 * @method void setToken(string $value)
36
 * @method string getType()
37
 * @method void setType(string $value)
38
 * @method int getPollId()
39
 * @method void setPollId(integer $value)
40
 * @method string getUserId()
41
 * @method void setUserId(string $value)
42
 * @method string getEmailAddress()
43
 * @method void setEmailAddress(string $value)
44
 * @method int getInvitationSent()
45
 * @method void setInvitationSent(integer $value)
46
 * @method string getDisplayName()
47
 * @method void setDisplayName(string $value)
48
 */
49
class Share extends Entity implements JsonSerializable {
50
51
	// Only authenticated access
52
	public const TYPE_USER = 'user';
53
	public const TYPE_GROUP = 'group';
54
55
	// Public and authenticated Access
56
	public const TYPE_PUBLIC = 'public';
57
58
	// Only public access
59
	public const TYPE_EMAIL = 'email';
60
	public const TYPE_CONTACT = 'contact';
61
	public const TYPE_EXTERNAL = 'external';
62
63
	// no direct Access
64
	public const TYPE_CIRCLE = 'circle';
65
	public const TYPE_CONTACTGROUP = 'contactGroup';
66
67
	/** @var string $token */
68
	protected $token;
69
70
	/** @var string $type */
71
	protected $type;
72
73
	/** @var int $pollId */
74
	protected $pollId;
75
76
	/** @var string $userId */
77
	protected $userId;
78
79
	/** @var string $emailAddress */
80
	protected $emailAddress;
81
82
	/** @var string $invitationSent */
83
	protected $invitationSent;
84
85
	/** @var string $displayName */
86
	protected $displayName;
87
88
	public function __construct() {
89
		$this->addType('pollId', 'int');
90
		$this->addType('invitationSent', 'int');
91
	}
92
93
	public function jsonSerialize() {
94
		return [
95
			'id' => $this->getId(),
96
			'token' => $this->getToken(),
97
			'type' => $this->getType(),
98
			'pollId' => $this->getPollId(),
99
			'userId' => $this->getUserId(),
100
			'emailAddress' => $this->getEmailAddress(),
101
			'invitationSent' => $this->getInvitationSent(),
102
			'displayName' => $this->getDisplayName(),
103
			'isNoUser' => !($this->getType() === self::TYPE_USER),
104
			'URL' => $this->getURL(),
105
		];
106
	}
107
108
	public function getURL(): string {
109
		if ($this->type === self::TYPE_USER || $this->type === self::TYPE_GROUP) {
110
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
111
				'polls.page.vote',
112
				['id' => $this->pollId]
113
			);
114
		} elseif ($this->token) {
115
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
116
				'polls.public.vote_page',
117
				['token' => $this->token]
118
			);
119
		} else {
120
			return '';
121
		}
122
	}
123
124
	public function getUserId(): string {
125
		if ($this->type === self::TYPE_CONTACTGROUP) {
126
			// contactsgroup had the prefix contactgroup_ until version 1.5
127
			// strip it out
128
			$parts = explode("contactgroup_", $this->userId);
129
			$userId = end($parts);
130
			return $userId;
131
		}
132
		return $this->userId;
133
	}
134
135
	public function getUserObject(): UserGroupClass {
136
		return UserGroupClass::getUserGroupChild(
137
			$this->type,
138
			$this->userId,
139
			$this->displayName,
140
			$this->emailAddress
141
		);
142
	}
143
144
	/**
145
	 * @return UserGroupClass[]
146
	 */
147
	public function getMembers() {
148
		if ($this->type === self::TYPE_GROUP
149
		|| $this->type === self::TYPE_CONTACTGROUP
150
		|| $this->type === self::TYPE_CIRCLE) {
151
			$group = UserGroupClass::getUserGroupChild($this->type, $this->getUserId());
152
			return $group->getMembers();
153
		} else {
154
			return [UserGroupClass::getUserGroupChild(
155
				$this->type,
156
				$this->userId,
157
				$this->displayName,
158
				$this->emailAddress
159
			)];
160
		}
161
	}
162
}
163