Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 87 |
|
0.00% |
0 / 17 |
CRAP | |
0.00% |
0 / 1 |
SystemController | |
0.00% |
0 / 87 |
|
0.00% |
0 / 17 |
702 | |
0.00% |
0 / 1 |
get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getWorkspaces | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
deleteWorkspaces | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getUsers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
deleteUsers | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getListRoutes | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
getVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getConfig | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
getStatus | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
patchAppConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
patchCustomTexts | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
updateMeta | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
getTime | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getSysChecks | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
getSysCheckMode | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getFlushBroadcastingService | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
postClearCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | |
4 | /** @noinspection PhpUnusedParameterInspection */ |
5 | declare(strict_types=1); |
6 | |
7 | // TODO unit tests ! |
8 | |
9 | use Slim\Exception\HttpBadRequestException; |
10 | use Slim\Http\Response; |
11 | use Slim\Http\ServerRequest as Request; |
12 | use Slim\Routing\Route; |
13 | |
14 | class SystemController extends Controller { |
15 | public static function get(Request $request, Response $response): Response { |
16 | return $response->withJson(['version' => SystemConfig::$system_version]); |
17 | } |
18 | |
19 | public static function getWorkspaces(Request $request, Response $response): Response { |
20 | $workspaces = self::superAdminDAO()->getWorkspaces(); |
21 | return $response->withJson($workspaces); |
22 | } |
23 | |
24 | public static function deleteWorkspaces(Request $request, Response $response): Response { |
25 | $bodyData = JSON::decode($request->getBody()->getContents()); |
26 | $workspaceList = $bodyData->ws ?? []; |
27 | |
28 | if (!is_array($workspaceList)) { |
29 | throw new HttpBadRequestException($request); |
30 | } |
31 | |
32 | self::superAdminDAO()->deleteWorkspaces($workspaceList); |
33 | |
34 | foreach ($workspaceList as $workspaceId) { |
35 | $workspace = new Workspace((int) $workspaceId); |
36 | $workspace->delete(); |
37 | } |
38 | |
39 | BroadcastService::send('system/clean'); |
40 | |
41 | return $response; |
42 | } |
43 | |
44 | public static function getUsers(Request $request, Response $response): Response { |
45 | return $response->withJson(self::superAdminDAO()->getUsers()); |
46 | } |
47 | |
48 | public static function deleteUsers(Request $request, Response $response): Response { |
49 | $bodyData = JSON::decode($request->getBody()->getContents()); |
50 | self::superAdminDAO()->deleteUsers($bodyData->u ?? []); |
51 | return $response; |
52 | } |
53 | |
54 | public static function getListRoutes(Request $request, Response $response): Response { |
55 | global $app; |
56 | $routes = array_reduce( |
57 | $app->getRouteCollector()->getRoutes(), |
58 | function ($target, Route $route) { |
59 | foreach ($route->getMethods() as $method) { |
60 | $target[] = "[$method] " . $route->getPattern(); |
61 | } |
62 | return $target; |
63 | }, |
64 | [] |
65 | ); |
66 | |
67 | return $response->withJson($routes); |
68 | } |
69 | |
70 | public static function getVersion(Request $request, Response $response): Response { |
71 | return $response->withJson(['version' => SystemConfig::$system_version]); |
72 | } |
73 | |
74 | public static function getConfig(Request $request, Response $response): Response { |
75 | $meta = self::adminDAO()->getMeta(['customTexts', 'appConfig']); |
76 | |
77 | return $response->withJson( |
78 | [ |
79 | 'version' => SystemConfig::$system_version, |
80 | 'customTexts' => (object) $meta['customTexts'], |
81 | 'appConfig' => (object) $meta['appConfig'], |
82 | 'baseUrl' => Server::getUrl(), |
83 | 'veronaPlayerApiVersionMin' => SystemConfig::$system_veronaMin, |
84 | 'veronaPlayerApiVersionMax' => SystemConfig::$system_veronaMax, |
85 | 'fileServiceUri' => FileService::getUri(), |
86 | 'broadcastingServiceUri' => BroadcastService::getUri() |
87 | ] |
88 | ); |
89 | } |
90 | |
91 | public static function getStatus(Request $request, Response $response): Response { |
92 | return $response->withJson([ |
93 | 'broadcastingService' => BroadcastService::getStatus(), |
94 | 'fileService' => FileService::getStatus(), |
95 | 'cacheService' => CacheService::getStatusFilesCache() |
96 | ]); |
97 | } |
98 | |
99 | public static function patchAppConfig(Request $request, Response $response): Response { |
100 | return SystemController::updateMeta('appConfig', $request, $response); |
101 | } |
102 | |
103 | public static function patchCustomTexts(Request $request, Response $response): Response { |
104 | return SystemController::updateMeta('customTexts', $request, $response); |
105 | } |
106 | |
107 | private static function updateMeta(string $category, Request $request, Response $response): Response { |
108 | $requestBody = JSON::decode($request->getBody()->getContents()); |
109 | |
110 | if (!is_object($requestBody)) { |
111 | return $response; |
112 | } |
113 | |
114 | foreach ($requestBody as $key => $value) { |
115 | $valueAsString = (is_string($value) or is_null($value)) ? $value : json_encode($value); |
116 | self::adminDAO()->setMeta($category, $key, $valueAsString); |
117 | } |
118 | |
119 | return $response; |
120 | } |
121 | |
122 | public static function getTime(Request $request, Response $response): Response { |
123 | return $response->withJson( |
124 | [ |
125 | 'timezone' => date_default_timezone_get(), |
126 | 'timestamp' => microtime(true) * 1000 |
127 | ] |
128 | ); |
129 | } |
130 | |
131 | public static function getSysChecks(Request $request, Response $response): Response { |
132 | $availableSysChecks = []; |
133 | |
134 | foreach (SysChecksFolder::getAll() as $sysChecksFolder) { |
135 | /* @var SysChecksFolder $sysChecksFolder */ |
136 | |
137 | $availableSysChecks = array_merge( |
138 | $availableSysChecks, |
139 | array_map( |
140 | function (XMLFileSysCheck $file) use ($sysChecksFolder) { |
141 | return [ |
142 | 'workspaceId' => $sysChecksFolder->getId(), |
143 | 'name' => $file->getId(), |
144 | 'label' => $file->getLabel(), |
145 | 'description' => $file->getDescription() |
146 | ]; |
147 | }, |
148 | $sysChecksFolder->findAvailableSysChecks() |
149 | ) |
150 | ); |
151 | } |
152 | |
153 | if (!count($availableSysChecks)) { |
154 | return $response->withStatus(204, "No SysChecks found."); |
155 | } |
156 | |
157 | return $response->withJson($availableSysChecks); |
158 | } |
159 | |
160 | public static function getSysCheckMode(Request $request, Response $response): Response { |
161 | $sysCheckModeExists = self::AdminDAO()->doesWSwitTypeSyscheckExist(); |
162 | |
163 | return $response->withJson($sysCheckModeExists); |
164 | } |
165 | |
166 | public static function getFlushBroadcastingService(Request $request, Response $response): Response { |
167 | BroadcastService::send('system/clean'); |
168 | return $response->withStatus(200); |
169 | } |
170 | |
171 | public static function postClearCache(Request $request, Response $response): Response { |
172 | return $response |
173 | ->withHeader('Clear-Site-Data', '"*"'); |
174 | } |
175 | } |