Completed
Pull Request — master (#120)
by Maxence
03:22
created

BroadcastService::broadcastFrame()   C

Complexity

Conditions 7
Paths 25

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
rs 6.7272
cc 7
eloc 21
nc 25
nop 1
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 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
			$circle = $this->circlesRequest->forceGetCircle(
107
				$frame->getCircle()
108
					  ->getUniqueId()
109
			);
110
111
			$broadcaster->init();
112
113
			if ($circle->getType() !== Circle::CIRCLES_PERSONAL) {
114
				$broadcaster->createShareToCircle($frame, $circle);
115
			}
116
117
			$members = $this->membersRequest->forceGetMembers(
118
				$circle->getUniqueId(), Member::LEVEL_MEMBER, true
119
			);
120
121
			foreach ($members AS $member) {
122
				$this->parseMember($member);
123
124
				if ($member->isBroadcasting()) {
125
					$broadcaster->createShareToMember($frame, $member);
126
				}
127
			}
128
		} catch (Exception $e) {
129
			throw $e;
130
		}
131
	}
132
133
134
	/**
135
	 * @param Member $member
136
	 */
137
	private function parseMember(Member &$member) {
138
		$this->parseMemberFromContact($member);
139
	}
140
141
142
	/**
143
	 * on Type Contact, we convert the type to MAIL and retreive the first mail of the list.
144
	 * If no email, we set the member as not broadcasting.
145
	 *
146
	 * @param Member $member
147
	 */
148
	private function parseMemberFromContact(Member &$member) {
149
150
		if ($member->getType() !== Member::TYPE_CONTACT) {
151
			return;
152
		}
153
154
		$contact = MiscService::getContactData($member->getUserId());
155
		if (!key_exists('EMAIL', $contact)) {
156
			$member->broadcasting(false);
157
158
			return;
159
		}
160
161
		$member->setType(Member::TYPE_MAIL);
162
		$member->setUserId(array_shift($contact['EMAIL']));
163
	}
164
165
166
}