Passed
Pull Request — master (#1219)
by René
08:30
created

Share::getValidAuthenticated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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
30
/**
31
 * @method string getId()
32
 * @method void setId(integer $value)
33
 * @method string getToken()
34
 * @method void setToken(string $value)
35
 * @method string getType()
36
 * @method void setType(string $value)
37
 * @method int getPollId()
38
 * @method void setPollId(integer $value)
39
 * @method string getUserId()
40
 * @method void setUserId(string $value)
41
 * @method string getEmailAddress()
42
 * @method void setEmailAddress(string $value)
43
 * @method int getInvitationSent()
44
 * @method void setInvitationSent(integer $value)
45
 * @method int getDisplayName()
46
 * @method void setDisplayName(string $value)
47
 */
48
class Share extends Entity implements JsonSerializable {
49
50
	// Only authenticated access
51
	public const TYPE_USER = 'user';
52
	public const TYPE_GROUP = 'group';
53
54
	// Public and authenticated Access
55
	public const TYPE_PUBLIC = 'public';
56
57
	// Only public access
58
	public const TYPE_EMAIL = 'email';
59
	public const TYPE_CONTACT = 'contact';
60
	public const TYPE_EXTERNAL = 'external';
61
62
	// no direct Access
63
	public const TYPE_CIRCLE = 'circle';
64
	public const TYPE_CONTACTGROUP = 'contactGroup';
65
66
	/** @var string $token */
67
	protected $token;
68
69
	/** @var string $type */
70
	protected $type;
71
72
	/** @var int $pollId */
73
	protected $pollId;
74
75
	/** @var string $userId */
76
	protected $userId;
77
78
	/** @var string $emailAddress */
79
	protected $emailAddress;
80
81
	/** @var string $invitationSent */
82
	protected $invitationSent;
83
84
	/** @var string $displayName */
85
	protected $displayName;
86
87
	public function jsonSerialize() {
88
		return [
89
			'id' => intval($this->id),
90
			'token' => $this->token,
91
			'type' => $this->type,
92
			'pollId' => intval($this->pollId),
93
			'userId' => $this->getUserId(),
94
			'emailAddress' => $this->emailAddress,
95
			'invitationSent' => intval($this->invitationSent),
96
			'displayName' => $this->displayName,
97
			'isNoUser' => !($this->type === self::TYPE_USER),
98
			'validPublic' => $this->getValidPublic(),
99
			'validAuthenticated' => $this->getValidAuthenticated(),
100
		];
101
	}
102
103
	public function getUserId() {
104
		if ($this->type === self::TYPE_CONTACTGROUP) {
105
			// contactsgroup had the prefix contactgroup_ until version 1.5
106
			// strip it out
107
			$parts = explode("contactgroup_", $this->userId);
108
			$userId = end($parts);
109
			return $userId;
110
		}
111
		return $this->userId;
112
	}
113
114
	public function getValidPublic() {
115
		return (
116
			   $this->type === self::TYPE_PUBLIC
117
			|| $this->type === self::TYPE_EMAIL
118
			|| $this->type === self::TYPE_CONTACT
119
			|| $this->type === self::TYPE_EXTERNAL);
120
	}
121
122
	public function getValidAuthenticated() {
123
		return (
124
			   $this->type === self::TYPE_PUBLIC
125
			|| $this->type === self::TYPE_USER
126
			|| $this->type === self::TYPE_GROUP);
127
	}
128
}
129