| Total Complexity | 57 |
| Total Lines | 380 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RadioApiController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RadioApiController, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 39 | class RadioApiController extends Controller { |
||
| 40 | private IConfig $config; |
||
| 41 | private IURLGenerator $urlGenerator; |
||
| 42 | private RadioStationBusinessLayer $businessLayer; |
||
| 43 | private RadioService $service; |
||
| 44 | private StreamTokenService $tokenService; |
||
| 45 | private PlaylistFileService $playlistFileService; |
||
| 46 | private string $userId; |
||
| 47 | private IRootFolder $rootFolder; |
||
| 48 | private Logger $logger; |
||
| 49 | |||
| 50 | public function __construct(string $appName, |
||
| 51 | IRequest $request, |
||
| 52 | IConfig $config, |
||
| 53 | IURLGenerator $urlGenerator, |
||
| 54 | RadioStationBusinessLayer $businessLayer, |
||
| 55 | RadioService $service, |
||
| 56 | StreamTokenService $tokenService, |
||
| 57 | PlaylistFileService $playlistFileService, |
||
| 58 | ?string $userId, |
||
| 59 | IRootFolder $rootFolder, |
||
| 60 | Logger $logger) { |
||
| 61 | parent::__construct($appName, $request); |
||
| 62 | $this->config = $config; |
||
| 63 | $this->urlGenerator = $urlGenerator; |
||
| 64 | $this->businessLayer = $businessLayer; |
||
| 65 | $this->service = $service; |
||
| 66 | $this->tokenService = $tokenService; |
||
| 67 | $this->playlistFileService = $playlistFileService; |
||
| 68 | $this->userId = $userId ?? ''; // ensure non-null to satisfy Scrutinizer; may be null when resolveStreamUrl used on public share |
||
| 69 | $this->rootFolder = $rootFolder; |
||
| 70 | $this->logger = $logger; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * lists all radio stations |
||
| 75 | * |
||
| 76 | * @NoAdminRequired |
||
| 77 | * @NoCSRFRequired |
||
| 78 | */ |
||
| 79 | public function getAll() : JSONResponse { |
||
| 80 | $stations = $this->businessLayer->findAll($this->userId); |
||
| 81 | return new JSONResponse( |
||
| 82 | \array_map(fn($s) => $s->toApi(), $stations) |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * creates a station |
||
| 88 | * |
||
| 89 | * @NoAdminRequired |
||
| 90 | * @NoCSRFRequired |
||
| 91 | */ |
||
| 92 | public function create(?string $name, ?string $streamUrl, ?string $homeUrl) : JSONResponse { |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * deletes a station |
||
| 107 | * |
||
| 108 | * @NoAdminRequired |
||
| 109 | * @NoCSRFRequired |
||
| 110 | */ |
||
| 111 | public function delete(int $id) : JSONResponse { |
||
| 112 | try { |
||
| 113 | $this->businessLayer->delete($id, $this->userId); |
||
| 114 | return new JSONResponse([]); |
||
| 115 | } catch (BusinessLayerException $ex) { |
||
| 116 | return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage()); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * get a single radio station |
||
| 122 | * |
||
| 123 | * @NoAdminRequired |
||
| 124 | * @NoCSRFRequired |
||
| 125 | */ |
||
| 126 | public function get(int $id) : JSONResponse { |
||
| 127 | try { |
||
| 128 | $station = $this->businessLayer->find($id, $this->userId); |
||
| 129 | return new JSONResponse($station->toApi()); |
||
| 130 | } catch (BusinessLayerException $ex) { |
||
| 131 | return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage()); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * update a station |
||
| 137 | * |
||
| 138 | * @NoAdminRequired |
||
| 139 | * @NoCSRFRequired |
||
| 140 | */ |
||
| 141 | public function update(int $id, ?string $name = null, ?string $streamUrl = null, ?string $homeUrl = null) : JSONResponse { |
||
| 142 | if ($name === null && $streamUrl === null && $homeUrl === null) { |
||
| 143 | return new ErrorResponse(Http::STATUS_BAD_REQUEST, "at least one of the args ['name', 'streamUrl', 'homeUrl'] must be given"); |
||
| 144 | } |
||
| 145 | |||
| 146 | try { |
||
| 147 | $station = $this->businessLayer->updateStation($id, $this->userId, $name, $streamUrl, $homeUrl); |
||
| 148 | return new JSONResponse($station->toApi()); |
||
| 149 | } catch (BusinessLayerException $ex) { |
||
| 150 | return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage()); |
||
| 151 | } catch (\DomainException $ex) { |
||
| 152 | return new ErrorResponse(Http::STATUS_BAD_REQUEST, $ex->getMessage()); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * export all radio stations to a file |
||
| 158 | * |
||
| 159 | * @param string $name target file name |
||
| 160 | * @param string $path parent folder path |
||
| 161 | * @param string $oncollision action to take on file name collision, |
||
| 162 | * supported values: |
||
| 163 | * - 'overwrite' The existing file will be overwritten |
||
| 164 | * - 'keepboth' The new file is named with a suffix to make it unique |
||
| 165 | * - 'abort' (default) The operation will fail |
||
| 166 | * |
||
| 167 | * @NoAdminRequired |
||
| 168 | * @NoCSRFRequired |
||
| 169 | */ |
||
| 170 | public function exportAllToFile(string $name, string $path, string $oncollision='abort') : JSONResponse { |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * import radio stations from a file |
||
| 187 | * @param string $filePath path of the file to import |
||
| 188 | * |
||
| 189 | * @NoAdminRequired |
||
| 190 | * @NoCSRFRequired |
||
| 191 | */ |
||
| 192 | public function importFromFile(string $filePath) : JSONResponse { |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * reset all the radio stations of the user |
||
| 207 | * |
||
| 208 | * @NoAdminRequired |
||
| 209 | * @NoCSRFRequired |
||
| 210 | */ |
||
| 211 | public function resetAll() : JSONResponse { |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * get metadata for a channel |
||
| 218 | * |
||
| 219 | * @NoAdminRequired |
||
| 220 | * @NoCSRFRequired |
||
| 221 | */ |
||
| 222 | public function getChannelInfo(int $id, ?string $type=null) : JSONResponse { |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * get stream URL for a radio station |
||
| 256 | * |
||
| 257 | * @NoAdminRequired |
||
| 258 | * @NoCSRFRequired |
||
| 259 | */ |
||
| 260 | public function stationStreamUrl(int $id) : JSONResponse { |
||
| 261 | try { |
||
| 262 | $station = $this->businessLayer->find($id, $this->userId); |
||
| 263 | return $this->doResolveStreamUrl($station->getStreamUrl()); |
||
| 264 | } catch (BusinessLayerException $ex) { |
||
| 265 | return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage()); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * get audio stream for a radio station |
||
| 271 | * |
||
| 272 | * @NoAdminRequired |
||
| 273 | * @NoCSRFRequired |
||
| 274 | */ |
||
| 275 | public function stationStream(int $id) : Response { |
||
| 276 | try { |
||
| 277 | $station = $this->businessLayer->find($id, $this->userId); |
||
| 278 | $streamUrl = $station->getStreamUrl(); |
||
| 279 | $resolved = $this->service->resolveStreamUrl($streamUrl); |
||
| 280 | if ($this->streamRelayEnabled()) { |
||
| 281 | return new RelayStreamResponse($resolved['url']); |
||
| 282 | } else { |
||
| 283 | return new RedirectResponse($resolved['url']); |
||
| 284 | } |
||
| 285 | } catch (BusinessLayerException $ex) { |
||
| 286 | return new ErrorResponse(Http::STATUS_NOT_FOUND, $ex->getMessage()); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * get the actual stream URL from the given public URL |
||
| 292 | * |
||
| 293 | * Available without login since no user data is handled and this may be used on link-shared folder. |
||
| 294 | * |
||
| 295 | * @PublicPage |
||
| 296 | * @NoCSRFRequired |
||
| 297 | */ |
||
| 298 | public function resolveStreamUrl(string $url, ?string $token) : JSONResponse { |
||
| 299 | $url = \rawurldecode($url); |
||
| 300 | |||
| 301 | if ($token === null) { |
||
| 302 | return new ErrorResponse(Http::STATUS_UNAUTHORIZED, 'a security token must be passed'); |
||
| 303 | } elseif (!$this->tokenService->urlTokenIsValid($url, \rawurldecode($token))) { |
||
| 304 | return new ErrorResponse(Http::STATUS_UNAUTHORIZED, 'the security token is invalid'); |
||
| 305 | } else { |
||
| 306 | return $this->doResolveStreamUrl($url); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | private function doResolveStreamUrl(string $url) : JSONResponse { |
||
| 311 | $resolved = $this->service->resolveStreamUrl($url); |
||
| 312 | $relayEnabled = $this->streamRelayEnabled(); |
||
| 313 | if ($resolved['url'] === null) { |
||
| 314 | return new ErrorResponse(Http::STATUS_NOT_FOUND, 'failed to read from the stream URL'); |
||
| 315 | } else { |
||
| 316 | $token = $this->tokenService->tokenForUrl($resolved['url']); |
||
| 317 | if ($resolved['hls']) { |
||
| 318 | $resolved['url'] = $this->urlGenerator->linkToRoute('music.radioApi.hlsManifest', |
||
| 319 | ['url' => \rawurlencode($resolved['url']), 'token' => \rawurlencode($token)]); |
||
| 320 | } elseif ($relayEnabled) { |
||
| 321 | $resolved['url'] = $this->urlGenerator->linkToRoute('music.radioApi.streamFromUrl', |
||
| 322 | ['url' => \rawurlencode($resolved['url']), 'token' => \rawurlencode($token)]); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | return new JSONResponse($resolved); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * create a relayed stream for the given URL if relaying enabled; otherwise just redirect to the URL |
||
| 330 | * |
||
| 331 | * @PublicPage |
||
| 332 | * @NoCSRFRequired |
||
| 333 | */ |
||
| 334 | public function streamFromUrl(string $url, ?string $token) : Response { |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * get manifest of a HLS stream |
||
| 350 | * |
||
| 351 | * This fetches the manifest file from the given URL and returns a modified version of it. |
||
| 352 | * The front-end can't easily stream directly from the original source because of the Content-Security-Policy. |
||
| 353 | * |
||
| 354 | * @PublicPage |
||
| 355 | * @NoCSRFRequired |
||
| 356 | */ |
||
| 357 | public function hlsManifest(string $url, ?string $token) : Response { |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * get one segment of a HLS stream |
||
| 379 | * |
||
| 380 | * The segment is fetched from the given URL and relayed as such to the client. |
||
| 381 | * |
||
| 382 | * @PublicPage |
||
| 383 | * @NoCSRFRequired |
||
| 384 | */ |
||
| 385 | public function hlsSegment(string $url, ?string $token) : Response { |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | private function hlsEnabled() : bool { |
||
| 406 | $enabled = (bool)$this->config->getSystemValue('music.enable_radio_hls', true); |
||
| 407 | if ($this->userId === '') { |
||
| 408 | $enabled = (bool)$this->config->getSystemValue('music.enable_radio_hls_on_share', $enabled); |
||
| 409 | } |
||
| 410 | return $enabled; |
||
| 411 | } |
||
| 412 | |||
| 413 | private function streamRelayEnabled() : bool { |
||
| 419 | } |
||
| 420 | } |
||
| 421 |