Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.82% covered (danger)
6.82%
3 / 44
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
BroadcastService
6.82% covered (danger)
6.82%
3 / 44
20.00% covered (danger)
20.00%
1 / 5
250.83
0.00% covered (danger)
0.00%
0 / 1
 getStatus
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 registerChannel
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 sessionChange
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 send
6.90% covered (danger)
6.90%
2 / 29
0.00% covered (danger)
0.00%
0 / 1
46.55
 getUri
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4// TODO unit-test
5// TODO find a way to integrate this in e2e-tests
6
7class BroadcastService {
8  static function getStatus(): string {
9    if (!SystemConfig::$broadcastingService_internal or !SystemConfig::$broadcastingService_external) {
10      return 'off';
11    }
12
13    $ping = BroadcastService::send('', '', 'GET');
14
15    return ($ping === null) ? 'unreachable' : 'on';
16  }
17
18  static function registerChannel(string $channelName, array $data): ?string {
19    $bsToken = md5((string) rand(0, 99999999));
20    $data['token'] = $bsToken;
21    $response = BroadcastService::send("$channelName/register", json_encode($data));
22    $url =
23      (SystemConfig::$system_tlsEnabled ? 'wss://' : 'ws://')
24      . SystemConfig::$broadcastingService_external
25      . "ws?token=$bsToken";
26    return ($response !== null) ? $url : null;
27  }
28
29  static function sessionChange(SessionChangeMessage $sessionChange): ?string {
30    return BroadcastService::send('push/session-change', json_encode($sessionChange));
31  }
32
33  static function send(string $endpoint, string $message = '', string $verb = "POST"): ?string {
34    if (!SystemConfig::$broadcastingService_internal or !SystemConfig::$broadcastingService_external) {
35      return null;
36    }
37
38    $curl = curl_init();
39
40    $bsUri = 'http://' . SystemConfig::$broadcastingService_internal;
41
42    $headers = ["Content-Type: application/json"];
43    if (TestEnvironment::$testMode) {
44      $headers[] = "Test-Mode: " . TestEnvironment::$testMode;
45    }
46
47    curl_setopt_array($curl, [
48      CURLOPT_URL => $bsUri . '/' . $endpoint,
49      CURLOPT_RETURNTRANSFER => true,
50      CURLOPT_ENCODING => "",
51      CURLOPT_MAXREDIRS => 10,
52      CURLOPT_TIMEOUT => 5,
53      CURLOPT_FOLLOWLOCATION => true,
54      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
55      CURLOPT_CUSTOMREQUEST => $verb,
56      CURLOPT_POSTFIELDS => $message,
57      CURLOPT_FAILONERROR => false, // allows to read body on error
58      CURLOPT_HTTPHEADER => $headers,
59    ]);
60
61    $curlResponse = curl_exec($curl);
62    $errorCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
63
64    if (($errorCode === 0) or ($curlResponse === false)) {
65      error_log("BroadcastingService responds Error on `[$verb$endpoint`: not available");
66      return null;
67    }
68
69    if ($errorCode >= 400) {
70      error_log("BroadcastingService responds Error on `[$verb$endpoint`: [$errorCode$curlResponse");
71      return null;
72    }
73
74    return $curlResponse;
75  }
76
77  public static function getUri(): string {
78    $proto = (SystemConfig::$system_tlsEnabled ? 'https://' : 'http://');
79    return $proto . SystemConfig::$broadcastingService_external;
80  }
81}