Completed
Pull Request — master (#597)
by Maxence
02:57
created

EventWrapperController::manageWrapper()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 4
nc 12
nop 1
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\Nextcloud\nc22\TNC22Controller;
34
use daita\MySmallPhpTools\Traits\TAsync;
35
use daita\MySmallPhpTools\Traits\TStringTools;
36
use Exception;
37
use OCA\Circles\Db\EventWrapperRequest;
38
use OCA\Circles\Model\Federated\EventWrapper;
39
use OCA\Circles\Model\Federated\FederatedEvent;
40
use OCA\Circles\Service\ConfigService;
41
use OCA\Circles\Service\FederatedEventService;
42
use OCA\Circles\Service\RemoteDownstreamService;
43
use OCA\Circles\Service\RemoteUpstreamService;
44
use OCP\AppFramework\Controller;
45
use OCP\AppFramework\Http;
46
use OCP\AppFramework\Http\DataResponse;
47
use OCP\IRequest;
48
49
50
/**
51
 * Class EventWrapperController
52
 *
53
 * @package OCA\Circles\Controller
54
 */
55
class EventWrapperController extends Controller {
56
57
58
	use TStringTools;
59
	use TAsync;
60
	use TNC22Controller;
61
62
63
	/** @var EventWrapperRequest */
64
	private $eventWrapperRequest;
65
66
	/** @var FederatedEventService */
67
	private $federatedEventService;
68
69
	/** @var RemoteUpstreamService */
70
	private $remoteUpstreamService;
71
72
	/** @var RemoteDownstreamService */
73
	private $remoteDownstreamService;
74
75
	/** @var ConfigService */
76
	private $configService;
77
78
79
	/**
80
	 * EventWrapperController constructor.
81
	 *
82
	 * @param string $appName
83
	 * @param IRequest $request
84
	 * @param EventWrapperRequest $eventWrapperRequest
85
	 * @param FederatedEventService $federatedEventService
86
	 * @param RemoteUpstreamService $remoteUpstreamService
87
	 * @param RemoteDownstreamService $remoteDownstreamService
88
	 * @param ConfigService $configService
89
	 */
90
	public function __construct(
91
		string $appName,
92
		IRequest $request,
93
		EventWrapperRequest $eventWrapperRequest,
94
		FederatedEventService $federatedEventService,
95
		RemoteUpstreamService $remoteUpstreamService,
96
		RemoteDownstreamService $remoteDownstreamService,
97
		ConfigService $configService
98
	) {
99
		parent::__construct($appName, $request);
100
		$this->eventWrapperRequest = $eventWrapperRequest;
101
		$this->federatedEventService = $federatedEventService;
102
		$this->remoteUpstreamService = $remoteUpstreamService;
103
		$this->remoteDownstreamService = $remoteDownstreamService;
104
		$this->configService = $configService;
105
	}
106
107
108
	/**
109
	 * Called locally.
110
	 *
111
	 * Async process and broadcast the event to every instances of GS
112
	 * This should be initiated by the instance that owns the Circles.
113
	 *
114
	 * @PublicPage
115
	 * @NoCSRFRequired
116
	 *
117
	 * @param string $token
118
	 *
119
	 * @return DataResponse
120
	 */
121
	public function asyncBroadcast(string $token): DataResponse {
122
		$wrappers = $this->remoteUpstreamService->getEventsByToken($token);
123
		if (empty($wrappers)) {
124
			return new DataResponse(null, Http::STATUS_OK);
125
		}
126
127
		// closing socket, keep current process running.
128
		$this->async();
129
130
		foreach ($wrappers as $wrapper) {
131
			$this->manageWrapper($wrapper);
132
		}
133
134
		$this->federatedEventService->manageResults($token);
135
136
		// exit() or useless log will be generated
137
		exit();
138
	}
139
140
141
//	/**
142
//	 * Status Event. This is an event to check status of items between instances.
143
//	 *
144
//	 * @PublicPage
145
//	 * @NoCSRFRequired
146
//	 *
147
//	 * @return DataResponse
148
//	 */
149
//	public function status(): DataResponse {
150
//		$data = file_get_contents('php://input');
151
//
152
//		try {
153
//			$event = new GSEvent();
154
//			$event->importFromJson($data);
155
//			$this->gsDownstreamService->statusEvent($event);
156
//
157
//			return $this->success(['success' => $event]);
158
//		} catch (Exception $e) {
159
//			return $this->fail(['data' => $data, 'error' => $e->getMessage()]);
160
//		}
161
//	}
162
163
164
	/**
165
	 * @param EventWrapper $wrapper
166
	 */
167
	private function manageWrapper(EventWrapper $wrapper): void {
168
		$status = EventWrapper::STATUS_FAILED;
169
170
		try {
171
			if ($this->configService->isLocalInstance($wrapper->getInstance())) {
172
				// TODO: verify that Result is updated
173
				$gs = $this->federatedEventService->getFederatedItem($wrapper->getEvent(), false);
174
				$gs->manage($wrapper->getEvent());
175
			} else {
176
				$this->remoteUpstreamService->broadcastEvent($wrapper);
177
			}
178
			$status = EventWrapper::STATUS_DONE;
179
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
180
		}
181
182
		if ($wrapper->getSeverity() === FederatedEvent::SEVERITY_HIGH) {
183
			$wrapper->setStatus($status);
184
		} else {
185
			$wrapper->setStatus(EventWrapper::STATUS_OVER);
186
		}
187
188
		$wrapper->setResult($wrapper->getEvent()->getResult());
189
		$this->eventWrapperRequest->update($wrapper);
190
	}
191
192
}
193
194