Passed
Pull Request — master (#1234)
by René
03:52
created

Share::getMembers()   A

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 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
			'validPublic' => $this->getValidPublic(),
100
			'validAuthenticated' => $this->getValidAuthenticated(),
101
			'user' => $this->getUserObject(),
102
			'members' => $this->getMembers(),
103
			'URL' => $this->getURL()
104
		];
105
	}
106
107
	public function getURL() {
108
		if ($this->type === self::TYPE_USER || $this->type === self::TYPE_GROUP) {
109
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
110
				'polls.page.vote',
111
				['id' => $this->pollId]
112
			);
113
		} else {
114
			return \OC::$server->getUrlGenerator()->linkToRouteAbsolute(
115
				'polls.page.vote_publicpublic',
116
				['token' => $this->token]
117
			);
118
		}
119
	}
120
	public function getUserId() {
121
		if ($this->type === self::TYPE_CONTACTGROUP) {
122
			// contactsgroup had the prefix contactgroup_ until version 1.5
123
			// strip it out
124
			$parts = explode("contactgroup_", $this->userId);
125
			$userId = end($parts);
126
			return $userId;
127
		}
128
		return $this->userId;
129
	}
130
131
	/**
132
	 * @return UserGroupClass
133
	 */
134
	public function getUserObject() {
135
		return UserGroupClass::getUserGroupChild(
136
			$this->type,
137
			$this->userId,
138
			$this->displayName,
139
			$this->emailAddress
140
		);
141
	}
142
143
	/**
144
	 * @return UserGroupClass[]
145
	 */
146
	public function getMembers() {
147
		if ($this->type === self::TYPE_GROUP
148
		|| $this->type === self::TYPE_CONTACTGROUP
149
		|| $this->type === self::TYPE_CIRCLE) {
150
			$group = UserGroupClass::getUserGroupChild($this->type, $this->getUserId());
151
			return $group->getMembers();
0 ignored issues
show
Bug introduced by
The method getMembers() does not exist on OCA\Polls\Model\Email. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
			return $group->/** @scrutinizer ignore-call */ getMembers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getMembers() does not exist on OCA\Polls\Model\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
			return $group->/** @scrutinizer ignore-call */ getMembers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getMembers() does not exist on OCA\Polls\Model\Contact. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
			return $group->/** @scrutinizer ignore-call */ getMembers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getMembers() does not exist on OCA\Polls\Model\GenericUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
			return $group->/** @scrutinizer ignore-call */ getMembers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
		} else {
153
			return [UserGroupClass::getUserGroupChild(
154
				$this->type,
155
				$this->userId,
156
				$this->displayName,
157
				$this->emailAddress
158
			)];
159
		}
160
	}
161
162
	/**
163
	 * @return bool
164
	 */
165
	public function getValidPublic() {
166
		return (
167
			   $this->type === self::TYPE_PUBLIC
168
			|| $this->type === self::TYPE_EMAIL
169
			|| $this->type === self::TYPE_CONTACT
170
			|| $this->type === self::TYPE_EXTERNAL);
171
	}
172
173
	/**
174
	 * @return bool
175
	 */
176
	public function getValidAuthenticated() {
177
		return (
178
			   $this->type === self::TYPE_PUBLIC
179
			|| $this->type === self::TYPE_USER
180
			|| $this->type === self::TYPE_GROUP);
181
	}
182
}
183