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

Share::getValidPublic()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 4
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 string 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 int 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 jsonSerialize() {
89
		return [
90
			'id' => intval($this->id),
91
			'token' => $this->token,
92
			'type' => $this->type,
93
			'pollId' => intval($this->pollId),
94
			'userId' => $this->getUserId(),
95
			'emailAddress' => $this->emailAddress,
96
			'invitationSent' => intval($this->invitationSent),
97
			'displayName' => $this->displayName,
98
			'isNoUser' => !($this->type === self::TYPE_USER),
99
			'URL' => $this->getURL()
100
		];
101
	}
102
103
	/**
104
	 * @return string
105
	 */
106
	public function getURL() {
107
		if ($this->type === self::TYPE_USER || $this->type === self::TYPE_GROUP) {
108
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
109
				'polls.page.vote',
110
				['id' => $this->pollId]
111
			);
112
		} elseif ($this->token) {
113
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
114
				'polls.page.vote_publicpublic',
115
				['token' => $this->token]
116
			);
117
		} else {
118
			return '';
119
		}
120
	}
121
122
	/**
123
	 * getUserId
124
	 * @return string
125
	 */
126
	public function getUserId() {
127
		if ($this->type === self::TYPE_CONTACTGROUP) {
128
			// contactsgroup had the prefix contactgroup_ until version 1.5
129
			// strip it out
130
			$parts = explode("contactgroup_", $this->userId);
131
			$userId = end($parts);
132
			return $userId;
133
		}
134
		return $this->userId;
135
	}
136
137
	/**
138
	 * getUserObject
139
	 * @return UserGroupClass
140
	 */
141
	public function getUserObject() {
142
		return UserGroupClass::getUserGroupChild(
143
			$this->type,
144
			$this->userId,
145
			$this->displayName,
146
			$this->emailAddress
147
		);
148
	}
149
150
	/**
151
	 * getMembers
152
	 * @return UserGroupClass[]
153
	 */
154
	public function getMembers() {
155
		if ($this->type === self::TYPE_GROUP
156
		|| $this->type === self::TYPE_CONTACTGROUP
157
		|| $this->type === self::TYPE_CIRCLE) {
158
			$group = UserGroupClass::getUserGroupChild($this->type, $this->getUserId());
159
			return $group->getMembers();
160
		} else {
161
			return [UserGroupClass::getUserGroupChild(
162
				$this->type,
163
				$this->userId,
164
				$this->displayName,
165
				$this->emailAddress
166
			)];
167
		}
168
	}
169
}
170