Completed
Pull Request — master (#347)
by Maxence
02:00
created

BroadcastService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.8333
cc 1
nc 1
nop 5
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
	 * BroadcastService 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 = $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 SharingFrame $frame
91
	 *
92
	 * @throws Exception
93
	 */
94
	public function broadcastFrame(SharingFrame $frame) {
95
96
		if ($frame->getHeader('broadcast') === null) {
97
			return;
98
		}
99
100
		try {
101
			$broadcaster = \OC::$server->query((string)$frame->getHeader('broadcast'));
102
			if (!($broadcaster instanceof IBroadcaster)) {
103
				throw new BroadcasterIsNotCompatibleException();
104
			}
105
106
			$frameCircle = $frame->getCircle();
107
			$circle = $this->circlesRequest->forceGetCircle($frameCircle->getUniqueId());
108
109
			$this->feedBroadcaster($broadcaster, $frame, $circle);
110
		} catch (Exception $e) {
111
			throw $e;
112
		}
113
	}
114
115
116
	/**
117
	 * @param IBroadcaster $broadcaster
118
	 * @param SharingFrame $frame
119
	 * @param Circle $circle
120
	 */
121
	private function feedBroadcaster(IBroadcaster $broadcaster, SharingFrame $frame, Circle $circle) {
122
		$broadcaster->init();
123
124
		if ($circle->getType() !== Circle::CIRCLES_PERSONAL) {
125
			$broadcaster->createShareToCircle($frame, $circle);
126
		}
127
128
		$members =
129
			$this->membersRequest->forceGetMembers($circle->getUniqueId(), Member::LEVEL_MEMBER, true);
130
		foreach ($members AS $member) {
131
			// removed so you can share to contacts
132
//			$this->parseMember($member);
133
134
			if ($member->isBroadcasting()) {
135
				$broadcaster->createShareToMember($frame, $member);
136
			}
137
		}
138
	}
139
140
141
	/**
142
	 * @param Member $member
143
	 */
144
	private function parseMember(Member &$member) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
145
		$this->parseMemberFromContact($member);
146
	}
147
148
149
	/**
150
	 * on Type Contact, we convert the type to MAIL and retreive the first mail of the list.
151
	 * If no email, we set the member as not broadcasting.
152
	 *
153
	 * @param Member $member
154
	 */
155
	private function parseMemberFromContact(Member &$member) {
156
157
		if ($member->getType() !== Member::TYPE_CONTACT) {
158
			return;
159
		}
160
161
		$contact = MiscService::getContactData($member->getUserId());
162
		if (!key_exists('EMAIL', $contact)) {
163
			$member->broadcasting(false);
164
165
			return;
166
		}
167
168
		$member->setType(Member::TYPE_MAIL);
169
		$member->setUserId(array_shift($contact['EMAIL']));
170
	}
171
172
}
173