Completed
Pull Request — master (#551)
by Maxence
02:15
created

CircleService::syncRemoteCircle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Service;
33
34
35
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
37
use daita\MySmallPhpTools\Exceptions\SignatoryException;
38
use daita\MySmallPhpTools\Model\Request;
39
use daita\MySmallPhpTools\Traits\TArrayTools;
40
use daita\MySmallPhpTools\Traits\TStringTools;
41
use OCA\Circles\Db\CircleRequest;
42
use OCA\Circles\Db\MemberRequest;
43
use OCA\Circles\Exceptions\CircleNotFoundException;
44
use OCA\Circles\Exceptions\FederatedEventException;
45
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
46
use OCA\Circles\Exceptions\InitiatorNotFoundException;
47
use OCA\Circles\Exceptions\InvalidIdException;
48
use OCA\Circles\Exceptions\MembersLimitException;
49
use OCA\Circles\Exceptions\OwnerNotFoundException;
50
use OCA\Circles\Exceptions\RemoteNotFoundException;
51
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
52
use OCA\Circles\Exceptions\UnknownRemoteException;
53
use OCA\Circles\FederatedItems\CircleCreate;
54
use OCA\Circles\Model\Circle;
55
use OCA\Circles\Model\Federated\FederatedEvent;
56
use OCA\Circles\Model\Federated\RemoteInstance;
57
use OCA\Circles\Model\FederatedUser;
58
use OCA\Circles\Model\ManagedModel;
59
use OCA\Circles\Model\Member;
60
61
62
/**
63
 * Class CircleService
64
 *
65
 * @package OCA\Circles\Service
66
 */
67
class CircleService {
68
69
70
	use TArrayTools;
71
	use TStringTools;
72
73
74
	/** @var CircleRequest */
75
	private $circleRequest;
76
77
	/** @var MemberRequest */
78
	private $memberRequest;
79
80
	/** @var RemoteStreamService */
81
	private $remoteStreamService;
82
83
	/** @var FederatedUserService */
84
	private $federatedUserService;
85
86
	/** @var FederatedEventService */
87
	private $federatedEventService;
88
89
	/** @var MemberService */
90
	private $memberService;
91
92
	/** @var ConfigService */
93
	private $configService;
94
95
96
	/**
97
	 * CircleService constructor.
98
	 *
99
	 * @param CircleRequest $circleRequest
100
	 * @param MemberRequest $memberRequest
101
	 * @param RemoteStreamService $remoteStreamService
102
	 * @param FederatedUserService $federatedUserService
103
	 * @param FederatedEventService $federatedEventService
104
	 * @param MemberService $memberService
105
	 * @param ConfigService $configService
106
	 */
107
	public function __construct(
108
		CircleRequest $circleRequest, MemberRequest $memberRequest, RemoteStreamService $remoteStreamService,
109
		FederatedUserService $federatedUserService, FederatedEventService $federatedEventService,
110
		MemberService $memberService, ConfigService $configService
111
	) {
112
		$this->circleRequest = $circleRequest;
113
		$this->memberRequest = $memberRequest;
114
		$this->remoteStreamService = $remoteStreamService;
115
		$this->federatedUserService = $federatedUserService;
116
		$this->federatedEventService = $federatedEventService;
117
		$this->memberService = $memberService;
118
		$this->configService = $configService;
119
	}
120
121
122
	/**
123
	 * @param string $name
124
	 * @param FederatedUser|null $owner
125
	 *
126
	 * @return Circle
127
	 * @throws OwnerNotFoundException
128
	 * @throws FederatedEventException
129
	 * @throws InitiatorNotFoundException
130
	 * @throws InitiatorNotConfirmedException
131
	 */
132
	public function create(string $name, ?FederatedUser $owner = null): Circle {
133
		$this->federatedUserService->mustHaveCurrentUser();
134
		if (is_null($owner)) {
135
			$owner = $this->federatedUserService->getCurrentUser();
136
		}
137
138
		$circle = new Circle();
139
		$circle->setName($name);
140
		$circle->setId($this->token(ManagedModel::ID_LENGTH));
141
142
		$member = new Member();
143
		$member->importFromIFederatedUser($owner);
0 ignored issues
show
Bug introduced by
It seems like $owner can be null; however, importFromIFederatedUser() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
144
		$member->setId($this->token(ManagedModel::ID_LENGTH))
145
			   ->setCircleId($circle->getId())
146
			   ->setLevel(Member::LEVEL_OWNER)
147
			   ->setStatus(Member::STATUS_MEMBER);
148
		$circle->setOwner($member)
149
			   ->setInitiator($member);
150
151
		$event = new FederatedEvent(CircleCreate::class);
152
		$event->setCircle($circle);
153
		$this->federatedEventService->newEvent($event);
154
155
		return $circle;
156
	}
157
158
159
	/**
160
	 * @param Member|null $filter
161
	 *
162
	 * @return Circle[]
163
	 * @throws InitiatorNotFoundException
164
	 */
165
	public function getCircles(?Member $filter = null): array {
166
		$this->federatedUserService->mustHaveCurrentUser();
167
168
		return $this->circleRequest->getCircles($filter, $this->federatedUserService->getCurrentUser());
169
	}
170
171
172
	/**
173
	 * @param string $circleId
174
	 *
175
	 * @return Circle
176
	 * @throws CircleNotFoundException
177
	 * @throws InitiatorNotFoundException
178
	 */
179
	public function getCircle(string $circleId): Circle {
180
		$this->federatedUserService->mustHaveCurrentUser();
181
182
		return $this->circleRequest->getCircle(
183
			$circleId,
184
			$this->federatedUserService->getCurrentUser(),
185
			$this->federatedUserService->getRemoteInstance()
186
		);
187
	}
188
189
190
	/**
191
	 * @param Circle $circle
192
	 *
193
	 * @throws MembersLimitException
194
	 */
195
	public function confirmCircleNotFull(Circle $circle): void {
196
		if ($this->isCircleFull($circle)) {
197
			throw new MembersLimitException('circle is full');
198
		}
199
	}
200
201
	/**
202
	 * @param Circle $circle
203
	 *
204
	 * @return bool
205
	 */
206
	public function isCircleFull(Circle $circle): bool {
207
		$members = $this->memberRequest->getMembers($circle->getId());
208
209
		$limit = $this->getInt('members_limit', $circle->getSettings());
210
		if ($limit === -1) {
211
			return false;
212
		}
213
		if ($limit === 0) {
214
			$limit = $this->configService->getAppValue(ConfigService::CIRCLES_MEMBERS_LIMIT);
215
		}
216
217
		return (sizeof($members) >= $limit);
218
	}
219
220
}
221
222