Completed
Pull Request — master (#106)
by Maxence
02:33
created

BroadcastService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 87
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B broadcastFrame() 0 29 6
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 Exception;
31
use OCA\Circles\Db\CirclesRequest;
32
use OCA\Circles\Db\MembersRequest;
33
use OCA\Circles\Exceptions\BroadcasterIsNotCompatibleException;
34
use OCA\Circles\IBroadcaster;
35
use OCA\Circles\Model\Circle;
36
use OCA\Circles\Model\Member;
37
use OCA\Circles\Model\SharingFrame;
38
39
40
class BroadcastService {
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var ConfigService */
46
	private $configService;
47
48
	/** @var CirclesRequest */
49
	private $circlesRequest;
50
51
	/** @var MembersRequest */
52
	private $membersRequest;
53
54
	/** @var MiscService */
55
	private $miscService;
56
57
58
	/**
59
	 * SharesService constructor.
60
	 *
61
	 * @param string $userId
62
	 * @param ConfigService $configService
63
	 * @param CirclesRequest $circlesRequest
64
	 * @param MembersRequest $membersRequest
65
	 * @param MiscService $miscService
66
	 */
67
	public function __construct(
68
		$userId,
69
		ConfigService $configService,
70
		CirclesRequest $circlesRequest,
71
		MembersRequest $membersRequest,
72
		MiscService $miscService
73
	) {
74
		$this->userId = (string)$userId;
75
		$this->configService = $configService;
76
		$this->circlesRequest = $circlesRequest;
77
		$this->membersRequest = $membersRequest;
78
		$this->miscService = $miscService;
79
	}
80
81
82
	/**
83
	 * broadcast the SharingFrame item using a IBroadcaster.
84
	 * The broadcast is usually set by the app that created the SharingFrame item.
85
	 *
86
	 * If the circle is not a Personal Circle, we first call createShareToCircle()
87
	 * Then for each members of the circle, we call createShareToUser()
88
	 * If the circle is a Personal Circle, we don't send data about the SharingFrame but null.
89
	 *
90
	 * @param string $broadcast
91
	 * @param SharingFrame $frame
92
	 *
93
	 * @throws Exception
94
	 */
95
	public function broadcastFrame($broadcast, SharingFrame $frame) {
96
97
		if ($broadcast === null) {
98
			return;
99
		}
100
101
		try {
102
			$broadcaster = \OC::$server->query((string)$broadcast);
103
			if (!($broadcaster instanceof IBroadcaster)) {
104
				throw new BroadcasterIsNotCompatibleException();
105
			}
106
107
			$circle = $this->circlesRequest->forceGetCircle($frame->getCircleId());
108
109
			$broadcaster->init();
110
			if ($circle->getType() !== Circle::CIRCLES_PERSONAL) {
111
				$broadcaster->createShareToCircle($frame);
112
			}
113
114
			$users = $this->membersRequest->forceGetMembers(
115
				$circle->getUniqueId(), Member::LEVEL_MEMBER, true
116
			);
117
			foreach ($users AS $user) {
118
				$broadcaster->createShareToUser($frame, $user->getUserId());
119
			}
120
		} catch (Exception $e) {
121
			throw $e;
122
		}
123
	}
124
125
126
}