Completed
Push — master ( 2079de...41fdc1 )
by Maxence
02:55
created

SharesService::generateHeaders()   A

Complexity

Conditions 2
Paths 8

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 11
nc 8
nop 3
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\Exceptions\BroadcasterIsNotCompatibleException;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
34
use OCA\Circles\Exceptions\SharingFrameAlreadyDeliveredException;
35
use OCA\Circles\Exceptions\SharingFrameDoesNotExistException;
36
use OCA\Circles\IBroadcaster;
37
use OCA\Circles\Model\Circle;
38
use OCA\Circles\Model\Member;
39
use OCA\Circles\Model\SharingFrame;
40
41
42
class SharesService {
43
44
	/** @var string */
45
	private $userId;
46
47
	/** @var ConfigService */
48
	private $configService;
49
50
	/** @var CirclesRequest */
51
	private $circlesRequest;
52
53
	/** @var BroadcastService */
54
	private $broadcastService;
55
56
	/** @var FederatedService */
57
	private $federatedService;
58
59
	/** @var MiscService */
60
	private $miscService;
61
62
63
	/**
64
	 * SharesService constructor.
65
	 *
66
	 * @param string $userId
67
	 * @param ConfigService $configService
68
	 * @param CirclesRequest $circlesRequest
69
	 * @param BroadcastService $broadcastService
70
	 * @param FederatedService $federatedService
71
	 * @param MiscService $miscService
72
	 */
73
	public function __construct(
74
		$userId,
75
		ConfigService $configService,
76
		CirclesRequest $circlesRequest,
77
		BroadcastService $broadcastService,
78
		FederatedService $federatedService,
79
		MiscService $miscService
80
	) {
81
		$this->userId = (string)$userId;
82
		$this->configService = $configService;
83
		$this->circlesRequest = $circlesRequest;
84
		$this->broadcastService = $broadcastService;
85
		$this->federatedService = $federatedService;
86
		$this->miscService = $miscService;
87
	}
88
89
90
	/**
91
	 * createFrame()
92
	 *
93
	 * Save the Frame containing the Payload.
94
	 * The Payload will be shared locally, and spread it live if a Broadcaster is set.
95
	 * Function will also initiate the federated broadcast to linked circles.
96
	 *
97
	 * @param string $circleUniqueId
98
	 * @param SharingFrame $frame
99
	 * @param string|null $broadcast
100
	 *
101
	 * @throws Exception
102
	 * @throws MemberDoesNotExistException
103
	 */
104
	public function createFrame($circleUniqueId, SharingFrame $frame, $broadcast = null) {
105
106
		try {
107
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
108
			$circle->getHigherViewer()
109
				   ->hasToBeMember();
110
111
			$frame->setCircle($circle);
112
113
			$this->generateHeaders($frame, $circle, $broadcast);
114
			$this->circlesRequest->saveFrame($frame);
115
116
			$this->broadcastService->broadcastFrame($frame->getHeader('broadcast'), $frame);
117
118
			if ($this->configService->isFederatedCirclesAllowed()) {
119
				$this->federatedService->initiateRemoteShare(
120
					$circle->getUniqueId(), $frame->getUniqueId()
121
				);
122
			}
123
		} catch (Exception $e) {
124
			throw $e;
125
		}
126
	}
127
128
129
	/**
130
	 * Generate Headers and few more thing like UniqueId and Author.
131
	 * Check if the source is NOT Circles.
132
	 *
133
	 * @param SharingFrame $frame
134
	 * @param Circle $circle
135
	 * @param $broadcast
136
	 */
137
	private function generateHeaders(SharingFrame $frame, Circle $circle, $broadcast) {
138
139
		try {
140
			$frame->cannotBeFromCircles();
141
142
			$frame->setAuthor($this->userId);
143
			$frame->setHeader('author', $this->userId);
144
			$frame->setHeader('circleName', $circle->getName());
145
			$frame->setHeader('circleUniqueId', $circle->getUniqueId());
146
			$frame->setHeader('broadcast', (string)$broadcast);
147
			$frame->generateUniqueId();
148
149
		} catch (Exception $e) {
150
			throw new $e;
151
		}
152
	}
153
154
	/**
155
	 * @param string $circleUniqueId
156
	 * @param string $frameUniqueId
157
	 *
158
	 * @return null|SharingFrame
159
	 * @throws SharingFrameAlreadyDeliveredException
160
	 * @throws SharingFrameDoesNotExistException
161
	 */
162
	public function getFrameFromUniqueId($circleUniqueId, $frameUniqueId) {
163
		if ($frameUniqueId === null || $frameUniqueId === '') {
164
			throw new SharingFrameDoesNotExistException('unknown_share');
165
		}
166
167
		try {
168
			$frame = $this->circlesRequest->getFrame($circleUniqueId, $frameUniqueId);
169
			if ($frame->getCloudId() !== null) {
170
				throw new SharingFrameAlreadyDeliveredException('share_already_delivered');
171
			}
172
		} catch (SharingFrameDoesNotExistException $e) {
173
			throw new SharingFrameDoesNotExistException('unknown_share');
174
		}
175
176
		return $frame;
177
	}
178
179
180
}