Completed
Pull Request — master (#362)
by Maxence
01:51
created

GlobalScaleController::broadcast()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 5
nop 0
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Controller;
31
32
33
use daita\MySmallPhpTools\Traits\TAsync;
34
use daita\MySmallPhpTools\Traits\TStringTools;
35
use Exception;
36
use OCA\Circles\Exceptions\GSStatusException;
37
use OCA\Circles\Model\GlobalScale\GSEvent;
38
use OCP\AppFramework\Http\DataResponse;
39
40
41
/**
42
 * Class GlobalScaleController
43
 *
44
 * @package OCA\Circles\Controller
45
 */
46
class GlobalScaleController extends BaseController {
47
48
49
	use TStringTools;
50
	use TAsync;
51
52
53
	/**
54
	 * Event is generated by any instance of GS and sent to the instance that owns the Circles, that
55
	 * will broadcast the event to other if ok
56
	 *
57
	 *
58
	 * @PublicPage
59
	 * @NoCSRFRequired
60
	 *
61
	 * @return DataResponse
62
	 */
63 View Code Duplication
	public function event(): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
		$data = file_get_contents('php://input');
65
66
		try {
67
			$event = new GSevent();
68
			$event->importFromJson($data);
69
			$this->gsDownstreamService->requestedEvent($event);
70
71
			return $this->success(['success' => $event]);
72
		} catch (Exception $e) {
73
			return $this->fail(['data' => $data, 'error' => $e->getMessage()]);
74
		}
75
	}
76
77
78
	/**
79
	 * Async process and broadcast the event to every instances of GS
80
	 * This should be initiated by the instance that owns the Circles.
81
	 *
82
	 * @PublicPage
83
	 * @NoCSRFRequired
84
	 *
85
	 * @param string $token
86
	 *
87
	 * @throws Exception
88
	 */
89
	public function asyncBroadcast(string $token) {
90
		try {
91
			$wrappers = $this->gsUpstreamService->getEventsByToken($token);
92
		} catch (Exception $e) {
93
			$this->miscService->log(
94
				'exception during async: ' . ['token' => $token, 'error' => $e->getMessage()]
95
			);
96
			$this->fail(['token' => $token, 'error' => $e->getMessage()]);
97
		}
98
99
		$this->async();
100
		foreach ($wrappers as $wrapper) {
101
			try {
102
				$this->gsUpstreamService->broadcastWrapper($wrapper, $this->request->getServerProtocol());
103
			} catch (GSStatusException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
104
			}
105
		}
106
107
		$this->gsUpstreamService->manageResults($token);
108
109
		exit();
110
	}
111
112
113
	/**
114
	 * Event is sent by instance that owns the Circles.
115
	 *
116
	 * @PublicPage
117
	 * @NoCSRFRequired
118
	 *
119
	 * @param string $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
120
	 *
121
	 * @return DataResponse
122
	 */
123 View Code Duplication
	public function broadcast(): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
		$data = file_get_contents('php://input');
125
126
		try {
127
			$event = new GSevent();
128
			$event->importFromJson($data);
129
130
			$this->gsDownstreamService->onNewEvent($event);
131
132
			return $this->success(['result' => $event->getResult()]);
133
		} catch (Exception $e) {
134
			return $this->fail(['data' => $data, 'error' => $e->getMessage()]);
135
		}
136
	}
137
138
139
	/**
140
	 * Status Event. This is an event to check status of items between instances.
141
	 *
142
	 * @PublicPage
143
	 * @NoCSRFRequired
144
	 *
145
	 * @return DataResponse
146
	 */
147 View Code Duplication
	public function status(): DataResponse {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
		$data = file_get_contents('php://input');
149
150
		try {
151
			$event = new GSevent();
152
			$event->importFromJson($data);
153
			$this->gsDownstreamService->statusEvent($event);
154
155
			return $this->success(['success' => $event]);
156
		} catch (Exception $e) {
157
			return $this->fail(['data' => $data, 'error' => $e->getMessage()]);
158
		}
159
	}
160
161
}
162
163