Completed
Push — master ( ec3803...963c88 )
by Maxence
02:31
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\TNC22Async;
34
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Controller;
35
use daita\MySmallPhpTools\Traits\TStringTools;
36
use OCA\Circles\AppInfo\Application;
37
use OCA\Circles\Service\ConfigService;
38
use OCA\Circles\Service\EventWrapperService;
39
use OCA\Circles\Service\FederatedEventService;
40
use OCA\Circles\Service\RemoteDownstreamService;
41
use OCA\Circles\Service\RemoteUpstreamService;
42
use OCP\AppFramework\Controller;
43
use OCP\AppFramework\Http;
44
use OCP\AppFramework\Http\DataResponse;
45
use OCP\IRequest;
46
47
48
/**
49
 * Class EventWrapperController
50
 *
51
 * @package OCA\Circles\Controller
52
 */
53
class EventWrapperController extends Controller {
54
55
56
	use TStringTools;
57
	use TNC22Async;
58
	use TNC22Controller;
59
60
61
	/** @var EventWrapperService */
62
	private $eventWrapperService;
63
64
	/** @var FederatedEventService */
65
	private $federatedEventService;
66
67
	/** @var RemoteUpstreamService */
68
	private $remoteUpstreamService;
69
70
	/** @var RemoteDownstreamService */
71
	private $remoteDownstreamService;
72
73
	/** @var ConfigService */
74
	private $configService;
75
76
77
	/**
78
	 * EventWrapperController constructor.
79
	 *
80
	 * @param string $appName
81
	 * @param IRequest $request
82
	 * @param EventWrapperService $eventWrapperService
83
	 * @param FederatedEventService $federatedEventService
84
	 * @param RemoteUpstreamService $remoteUpstreamService
85
	 * @param RemoteDownstreamService $remoteDownstreamService
86
	 * @param ConfigService $configService
87
	 */
88
	public function __construct(
89
		string $appName,
90
		IRequest $request,
91
		EventWrapperService $eventWrapperService,
92
		FederatedEventService $federatedEventService,
93
		RemoteUpstreamService $remoteUpstreamService,
94
		RemoteDownstreamService $remoteDownstreamService,
95
		ConfigService $configService
96
	) {
97
		parent::__construct($appName, $request);
98
		$this->eventWrapperService = $eventWrapperService;
99
		$this->federatedEventService = $federatedEventService;
100
		$this->remoteUpstreamService = $remoteUpstreamService;
101
		$this->remoteDownstreamService = $remoteDownstreamService;
102
		$this->configService = $configService;
103
104
		$this->setup('app', Application::APP_ID);
105
		$this->setupInt(self::$SETUP_TIME_LIMIT, 900);
106
	}
107
108
109
	/**
110
	 * Called locally.
111
	 *
112
	 * Async process and broadcast the event to every instances of GS
113
	 * This should be initiated by the instance that owns the Circles.
114
	 *
115
	 * @PublicPage
116
	 * @NoCSRFRequired
117
	 *
118
	 * @param string $token
119
	 *
120
	 * @return DataResponse
121
	 */
122
	public function asyncBroadcast(string $token): DataResponse {
123
		$wrappers = $this->remoteUpstreamService->getEventsByToken($token);
124
		if (empty($wrappers) && $token !== 'test-dummy-token') {
125
			return new DataResponse(null, Http::STATUS_OK);
126
		}
127
128
		// closing socket, keep current process running.
129
		$this->async();
130
131
		foreach ($wrappers as $wrapper) {
132
			$this->eventWrapperService->manageWrapper($wrapper);
133
		}
134
135
		$this->eventWrapperService->confirmStatus($token);
136
137
		// so circles:check can check async is fine
138
		if ($token === 'test-dummy-token') {
139
			sleep(5);
140
		}
141
142
		// exit() or useless log will be generated
143
		exit();
144
	}
145
146
147
//	/**
148
//	 * Status Event. This is an event to check status of items between instances.
149
//	 *
150
//	 * @PublicPage
151
//	 * @NoCSRFRequired
152
//	 *
153
//	 * @return DataResponse
154
//	 */
155
//	public function status(): DataResponse {
156
//		$data = file_get_contents('php://input');
157
//
158
//		try {
159
//			$event = new GSEvent();
160
//			$event->importFromJson($data);
161
//			$this->gsDownstreamService->statusEvent($event);
162
//
163
//			return $this->success(['success' => $event]);
164
//		} catch (Exception $e) {
165
//			return $this->fail(['data' => $data, 'error' => $e->getMessage()]);
166
//		}
167
//	}
168
169
}
170
171