Completed
Push — master ( 2cae30...13b58f )
by Maxence
03:19
created

SharesService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B createFrame() 0 24 4
A generateHeaders() 0 16 2
A getFrameFromUniqueId() 0 7 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\BroadcasterIsNotCompatible;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
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 SharesService {
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var ConfigService */
46
	private $configService;
47
48
	/** @var CirclesRequest */
49
	private $circlesRequest;
50
51
	/** @var BroadcastService */
52
	private $broadcastService;
53
54
	/** @var FederatedService */
55
	private $federatedService;
56
57
	/** @var MiscService */
58
	private $miscService;
59
60
61
	/**
62
	 * SharesService constructor.
63
	 *
64
	 * @param string $userId
65
	 * @param ConfigService $configService
66
	 * @param CirclesRequest $circlesRequest
67
	 * @param BroadcastService $broadcastService
68
	 * @param FederatedService $federatedService
69
	 * @param MiscService $miscService
70
	 */
71
	public function __construct(
72
		$userId,
73
		ConfigService $configService,
74
		CirclesRequest $circlesRequest,
75
		BroadcastService $broadcastService,
76
		FederatedService $federatedService,
77
		MiscService $miscService
78
	) {
79
		$this->userId = (string)$userId;
80
		$this->configService = $configService;
81
		$this->circlesRequest = $circlesRequest;
82
		$this->broadcastService = $broadcastService;
83
		$this->federatedService = $federatedService;
84
		$this->miscService = $miscService;
85
	}
86
87
88
	/**
89
	 * createFrame()
90
	 *
91
	 * Save the Frame containing the Payload.
92
	 * The Payload will be shared locally, and spread it live if a Broadcaster is set.
93
	 * Function will also initiate the federated broadcast to linked circles.
94
	 *
95
	 * @param SharingFrame $frame
96
	 * @param string|null $broadcast
97
	 *
98
	 * @throws Exception
99
	 */
100
	public function createFrame(SharingFrame $frame, $broadcast = null) {
101
102
		$circle = $this->circlesRequest->getCircleFromId($frame->getCircleId(), $this->userId);
103
		if ($circle->getUser()
104
				   ->getLevel() < Member::LEVEL_MEMBER
105
		) {
106
			throw new MemberDoesNotExistException();
107
		}
108
109
		try {
110
			$this->generateHeaders($frame, $circle, $broadcast);
0 ignored issues
show
Bug introduced by
It seems like $circle defined by $this->circlesRequest->g...cleId(), $this->userId) on line 102 can be null; however, OCA\Circles\Service\Shar...vice::generateHeaders() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
111
			$this->circlesRequest->saveFrame($frame);
112
113
			$this->broadcastService->broadcastFrame($frame->getHeader('broadcast'), $frame);
114
115
			if ($this->configService->isFederatedAllowed()) {
116
				$this->federatedService->initiateRemoteShare(
117
					$circle->getId(), $frame->getUniqueId()
118
				);
119
			}
120
		} catch (Exception $e) {
121
			throw $e;
122
		}
123
	}
124
125
126
	/**
127
	 * Generate Headers and few more thing like UniqueId and Author.
128
	 * Check if the source is NOT Circles.
129
	 *
130
	 * @param SharingFrame $frame
131
	 * @param Circle $circle
132
	 * @param $broadcast
133
	 */
134
	private function generateHeaders(SharingFrame $frame, Circle $circle, $broadcast) {
135
136
		try {
137
			$frame->cannotBeFromCircles();
138
139
			$frame->setAuthor($this->userId);
140
			$frame->setHeader('author', $this->userId);
141
			$frame->setHeader('circleName', $circle->getName());
142
			$frame->setHeader('broadcast', (string)$broadcast);
143
			$frame->generateUniqueId();
144
			$frame->setCircleName($circle->getName());
145
146
		} catch (Exception $e) {
147
			throw new $e;
148
		}
149
	}
150
151
	/**
152
	 * @param int $circleId
153
	 * @param $uniqueId
154
	 *
155
	 * @return null|SharingFrame
156
	 */
157
	public function getFrameFromUniqueId($circleId, $uniqueId) {
158
		if ($uniqueId === null || $uniqueId === '') {
159
			return null;
160
		}
161
162
		return $this->circlesRequest->getFrame((int)$circleId, (string)$uniqueId);
163
	}
164
165
166
}