Completed
Push — federated-circles ( f4a386...1e2c73 )
by Maxence
02:40
created

SharesService::broadcastFrame()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Service;
28
29
30
use OCA\Circles\Db\CirclesRequest;
31
use OCA\Circles\Exceptions\BroadcasterIsNotCompatible;
32
use OCA\Circles\Exceptions\MemberDoesNotExistException;
33
use OCA\Circles\IBroadcaster;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Model\SharingFrame;
36
37
38
class SharesService {
39
40
	/** @var string */
41
	private $userId;
42
	
43
	/** @var ConfigService */
44
	private $configService;
45
46
	/** @var CirclesRequest */
47
	private $circlesRequest;
48
49
	/** @var FederatedService */
50
	private $federatedService;
51
	/** @var MiscService */
52
	private $miscService;
53
54
55
	/**
56
	 * SharesService constructor.
57
	 *
58
	 * @param string $userId
59
	 * @param ConfigService $configService
60
	 * @param CirclesRequest $circlesRequest
61
	 * @param FederatedService $federatedService
62
	 * @param MiscService $miscService
63
	 */
64
	public function __construct(
65
		string $userId,
66
		ConfigService $configService,
0 ignored issues
show
Unused Code introduced by
The parameter $configService is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
		CirclesRequest $circlesRequest,
68
		FederatedService $federatedService,
69
		MiscService $miscService
70
	) {
71
		$this->userId = $userId;
72
		$this->circlesRequest = $circlesRequest;
73
		$this->federatedService = $federatedService;
74
		$this->miscService = $miscService;
75
	}
76
77
78
	/**
79
	 * createFrame()
80
	 *
81
	 * Save the Frame containing the Payload.
82
	 * The Payload will be shared locally, and spread it live if a Broadcaster is set.
83
	 * Function will also initiate the federated broadcast to linked circles.
84
	 *
85
	 * @param SharingFrame $frame
86
	 * @param string|null $broadcast
87
	 *
88
	 * @return bool
89
	 * @throws MemberDoesNotExistException
90
	 */
91
	public function createFrame(SharingFrame $frame, string $broadcast = null) {
92
93
		$circle = $this->circlesRequest->getDetails($frame->getCircleId(), $this->userId);
94
		if ($circle->getUser()
95
				   ->getLevel() < Member::LEVEL_MEMBER
96
		) {
97
			throw new MemberDoesNotExistException();
98
		}
99
100
		$frame->setAuthor($this->userId);
101
		$frame->generateUniqueId();
102
		$frame->setCircleName($circle->getName());
103
104
		$this->circlesRequest->saveFrame($frame);
105
		$this->broadcastFrame($broadcast, $frame);
106
107
		if ($this->configService->isFederatedAllowed()) {
108
			$this->federatedService->initiateRemoteShare($frame->getUniqueId());
109
		}
110
111
		return true;
112
	}
113
114
115
	public function getFrameFromUniqueId($uniqueId) {
116
		if ($uniqueId === null || $uniqueId === '') {
117
			return null;
118
		}
119
120
		return $this->circlesRequest->getFrame($uniqueId);
121
	}
122
123
124
	/**
125
	 * broadcast the SharingFrame item using a IBroadcaster.
126
	 * The broadcast is usually set by the app that created the SharingFrame item.
127
	 *
128
	 * @param string $broadcast
129
	 * @param SharingFrame $frame
130
	 *
131
	 * @throws BroadcasterIsNotCompatible
132
	 */
133
	private function broadcastFrame(string $broadcast, SharingFrame $frame) {
134
135
		if ($broadcast === null) {
136
			return;
137
		}
138
139
		$broadcaster = \OC::$server->query($broadcast);
140
		if (!($broadcaster instanceof IBroadcaster)) {
141
			throw new BroadcasterIsNotCompatible();
142
		}
143
144
		$broadcaster->init();
145
		$users = $this->circlesRequest->getMembers($frame->getCircleId(), Member::LEVEL_MEMBER);
146
		foreach ($users AS $user) {
147
			$broadcaster->broadcast($user->getUserId(), $frame);
148
		}
149
150
	}
151
152
}