Completed
Push — federated-circles ( 450355...2a3998 )
by Maxence
04:51 queued 02:17
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 CirclesRequest */
44
	private $circlesRequest;
45
46
	/** @var FederatedService */
47
	private $federatedService;
48
	/** @var MiscService */
49
	private $miscService;
50
51
52
	/**
53
	 * SharesService constructor.
54
	 *
55
	 * @param string $userId
56
	 * @param CirclesRequest $circlesRequest
57
	 * @param FederatedService $federatedService
58
	 * @param MiscService $miscService
59
	 */
60
	public function __construct(
61
		string $userId,
62
		CirclesRequest $circlesRequest,
63
		FederatedService $federatedService,
64
		MiscService $miscService
65
	) {
66
		$this->userId = $userId;
67
		$this->circlesRequest = $circlesRequest;
68
		$this->federatedService = $federatedService;
69
		$this->miscService = $miscService;
70
	}
71
72
73
	/**
74
	 * createFrame()
75
	 *
76
	 * Save the Frame containing the Payload.
77
	 * The Payload will be shared locally, and spread it live if a Broadcaster is set.
78
	 * Function will also initiate the federated broadcast to linked circles.
79
	 *
80
	 * @param SharingFrame $frame
81
	 * @param string|null $broadcast
82
	 *
83
	 * @return bool
84
	 * @throws MemberDoesNotExistException
85
	 */
86
	public function createFrame(SharingFrame $frame, string $broadcast = null) {
87
88
		$circle = $this->circlesRequest->getDetails($frame->getCircleId(), $this->userId);
89
		if ($circle->getUser()
90
				   ->getLevel() < Member::LEVEL_MEMBER
91
		) {
92
			throw new MemberDoesNotExistException();
93
		}
94
95
		$frame->setAuthor($this->userId);
96
		$frame->generateUniqueId();
97
		$frame->setCircleName($circle->getName());
98
99
		$this->circlesRequest->saveFrame($frame);
100
		$this->broadcastFrame($broadcast, $frame);
101
		$this->federatedService->initiateRemoteShare($frame->getUniqueId());
102
103
		return true;
104
	}
105
106
107
	public function getFrameFromUniqueId($uniqueId) {
108
		$this->circlesRequest->getFrame($uniqueId);
109
	}
110
111
112
	/**
113
	 * broadcast the SharingFrame item using a IBroadcaster.
114
	 * The broadcast is usually set by the app that created the SharingFrame item.
115
	 *
116
	 * @param string $broadcast
117
	 * @param SharingFrame $frame
118
	 *
119
	 * @throws BroadcasterIsNotCompatible
120
	 */
121
	private function broadcastFrame(string $broadcast, SharingFrame $frame) {
122
123
		if ($broadcast === null) {
124
			return;
125
		}
126
127
		$broadcaster = \OC::$server->query($broadcast);
128
		if (!($broadcaster instanceof IBroadcaster)) {
129
			throw new BroadcasterIsNotCompatible();
130
		}
131
132
		$broadcaster->init();
133
		$users = $this->circlesRequest->getMembers($frame->getCircleId(), Member::LEVEL_MEMBER);
134
		foreach ($users AS $user) {
135
			$broadcaster->broadcast($user->getUserId(), $frame);
136
		}
137
138
	}
139
140
141
	public function shareItemToFederatedLinks(SharingFrame $share, string $broadcast = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $share 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...
Unused Code introduced by
The parameter $broadcast 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...
142
143
		//$circles = $this->circlesRequest->getFederatedLinks($share->getCircle());
144
// TODO, envoyer une requete http sur le broadcaster local en precisant qu'il a deja ete broadcaste en local
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
145
		//broadcastItem()
146
	}
147
148
149
150
//	public function reshare($circleId, $source, $type, $shareid) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
151
//		$this->miscService->log(
152
//			"__reshare" . $circleId . ' ' . $source . ' ' . $type . ' ' . json_encode($item)
153
//		);
154
//
155
//		$share = new Share($source, $type);
156
//		$share->setCircleId($circleId);
157
//		$share->setItem($item);
158
//
159
//		$share->setAuthor($this->userId);
160
//
161
//		$this->circlesRequest->createShare($share);
162
//
163
//		return true;
164
//	}
165
166
}