|
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
|
|
|
* @PublicPage |
|
58
|
|
|
* @NoCSRFRequired |
|
59
|
|
|
* |
|
60
|
|
|
* @return DataResponse |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function event(): DataResponse { |
|
|
|
|
|
|
63
|
|
|
$data = file_get_contents('php://input'); |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
$event = new GSEvent(); |
|
67
|
|
|
$event->importFromJson($data); |
|
68
|
|
|
$this->gsDownstreamService->requestedEvent($event); |
|
69
|
|
|
|
|
70
|
|
|
return $this->success(['success' => $event]); |
|
71
|
|
|
} catch (Exception $e) { |
|
72
|
|
|
return $this->fail(['data' => $data, 'error' => $e->getMessage()]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Async process and broadcast the event to every instances of GS |
|
79
|
|
|
* This should be initiated by the instance that owns the Circles. |
|
80
|
|
|
* |
|
81
|
|
|
* @PublicPage |
|
82
|
|
|
* @NoCSRFRequired |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $token |
|
85
|
|
|
* |
|
86
|
|
|
* @throws Exception |
|
87
|
|
|
*/ |
|
88
|
|
|
public function asyncBroadcast(string $token) { |
|
89
|
|
|
try { |
|
90
|
|
|
$wrappers = $this->gsUpstreamService->getEventsByToken($token); |
|
91
|
|
|
} catch (Exception $e) { |
|
92
|
|
|
$this->miscService->log( |
|
93
|
|
|
'exception during async: ' . ['token' => $token, 'error' => $e->getMessage()] |
|
94
|
|
|
); |
|
95
|
|
|
$this->fail(['token' => $token, 'error' => $e->getMessage()]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->async(); |
|
99
|
|
|
foreach ($wrappers as $wrapper) { |
|
100
|
|
|
try { |
|
101
|
|
|
$this->gsUpstreamService->broadcastWrapper($wrapper, $this->request->getServerProtocol()); |
|
102
|
|
|
} catch (GSStatusException $e) { |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->gsUpstreamService->manageResults($token); |
|
107
|
|
|
|
|
108
|
|
|
exit(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Event is sent by instance that owns the Circles. |
|
114
|
|
|
* |
|
115
|
|
|
* @PublicPage |
|
116
|
|
|
* @NoCSRFRequired |
|
117
|
|
|
* |
|
118
|
|
|
* @return DataResponse |
|
119
|
|
|
*/ |
|
120
|
|
View Code Duplication |
public function broadcast(): DataResponse { |
|
|
|
|
|
|
121
|
|
|
$data = file_get_contents('php://input'); |
|
122
|
|
|
|
|
123
|
|
|
try { |
|
124
|
|
|
$event = new GSEvent(); |
|
125
|
|
|
$event->importFromJson($data); |
|
126
|
|
|
|
|
127
|
|
|
$this->gsDownstreamService->onNewEvent($event); |
|
128
|
|
|
|
|
129
|
|
|
return $this->success(['result' => $event->getResult()]); |
|
130
|
|
|
} catch (Exception $e) { |
|
131
|
|
|
return $this->fail(['data' => $data, 'error' => $e->getMessage()]); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Status Event. This is an event to check status of items between instances. |
|
138
|
|
|
* |
|
139
|
|
|
* @PublicPage |
|
140
|
|
|
* @NoCSRFRequired |
|
141
|
|
|
* |
|
142
|
|
|
* @return DataResponse |
|
143
|
|
|
*/ |
|
144
|
|
View Code Duplication |
public function status(): DataResponse { |
|
|
|
|
|
|
145
|
|
|
$data = file_get_contents('php://input'); |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
$event = new GSEvent(); |
|
149
|
|
|
$event->importFromJson($data); |
|
150
|
|
|
$this->gsDownstreamService->statusEvent($event); |
|
151
|
|
|
|
|
152
|
|
|
return $this->success(['success' => $event]); |
|
153
|
|
|
} catch (Exception $e) { |
|
154
|
|
|
return $this->fail(['data' => $data, 'error' => $e->getMessage()]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
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.