Completed
Push — federated-circles ( 1e2c73...673aab )
by Maxence
02:33
created

SharesService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 123
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B createFrame() 0 24 3
A getFrameFromUniqueId() 0 7 3
A broadcastFrame() 0 18 4
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,
67
		CirclesRequest $circlesRequest,
68
		FederatedService $federatedService,
69
		MiscService $miscService
70
	) {
71
		$this->userId = $userId;
72
		$this->configService = $configService;
73
		$this->circlesRequest = $circlesRequest;
74
		$this->federatedService = $federatedService;
75
		$this->miscService = $miscService;
76
	}
77
78
79
	/**
80
	 * createFrame()
81
	 *
82
	 * Save the Frame containing the Payload.
83
	 * The Payload will be shared locally, and spread it live if a Broadcaster is set.
84
	 * Function will also initiate the federated broadcast to linked circles.
85
	 *
86
	 * @param SharingFrame $frame
87
	 * @param string|null $broadcast
88
	 *
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->setHeader('author', $this->userId);
102
		$frame->setHeader('circleName', $circle->getName());
103
		$frame->setHeader('broadcast', $broadcast);
104
		$frame->generateUniqueId();
105
		$frame->setCircleName($circle->getName());
106
107
		$this->circlesRequest->saveFrame($frame);
108
109
		$this->broadcastFrame($frame->getHeader('broadcast'), $frame);
110
111
		if ($this->configService->isFederatedAllowed()) {
112
			$this->federatedService->initiateRemoteShare($circle->getId(), $frame->getUniqueId());
113
		}
114
	}
115
116
117
	/**
118
	 * @param int $circleId
119
	 * @param $uniqueId
120
	 *
121
	 * @return null|SharingFrame
122
	 */
123
	public function getFrameFromUniqueId(int $circleId, $uniqueId) {
124
		if ($uniqueId === null || $uniqueId === '') {
125
			return null;
126
		}
127
128
		return $this->circlesRequest->getFrame($circleId, $uniqueId);
129
	}
130
131
132
	/**
133
	 * broadcast the SharingFrame item using a IBroadcaster.
134
	 * The broadcast is usually set by the app that created the SharingFrame item.
135
	 *
136
	 * @param string $broadcast
137
	 * @param SharingFrame $frame
138
	 *
139
	 * @throws BroadcasterIsNotCompatible
140
	 */
141
	private function broadcastFrame(string $broadcast, SharingFrame $frame) {
142
143
		if ($broadcast === null) {
144
			return;
145
		}
146
147
		$broadcaster = \OC::$server->query($broadcast);
148
		if (!($broadcaster instanceof IBroadcaster)) {
149
			throw new BroadcasterIsNotCompatible();
150
		}
151
152
		$broadcaster->init();
153
		$users = $this->circlesRequest->getMembers($frame->getCircleId(), Member::LEVEL_MEMBER);
154
		foreach ($users AS $user) {
155
			$broadcaster->broadcast($user->getUserId(), $frame);
156
		}
157
158
	}
159
160
}