| Total Complexity | 54 |
| Total Lines | 336 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RadioService 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 RadioService, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 27 | class RadioService { |
||
| 28 | |||
| 29 | private IURLGenerator $urlGenerator; |
||
| 30 | private StreamTokenService $tokenService; |
||
| 31 | private Logger $logger; |
||
| 32 | |||
| 33 | public function __construct(IURLGenerator $urlGenerator, StreamTokenService $tokenService, Logger $logger) { |
||
| 34 | $this->urlGenerator = $urlGenerator; |
||
| 35 | $this->tokenService = $tokenService; |
||
| 36 | $this->logger = $logger; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Loop through the array and try to find the given key. On match, return the |
||
| 41 | * text in the array cell following the key. Whitespace is trimmed from the result. |
||
| 42 | */ |
||
| 43 | private static function findStrFollowing(array $data, string $key) : ?string { |
||
| 44 | foreach ($data as $value) { |
||
| 45 | $find = \strstr($value, $key); |
||
| 46 | if ($find !== false) { |
||
| 47 | return \trim(\substr($find, \strlen($key))); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | private static function parseStreamUrl(string $url) : array { |
||
| 54 | $ret = []; |
||
| 55 | $parse_url = \parse_url($url); |
||
| 56 | |||
| 57 | $ret['port'] = 80; |
||
| 58 | if (isset($parse_url['port'])) { |
||
| 59 | $ret['port'] = $parse_url['port']; |
||
| 60 | } elseif ($parse_url['scheme'] == "https") { |
||
| 61 | $ret['port'] = 443; |
||
| 62 | } |
||
| 63 | |||
| 64 | $ret['scheme'] = $parse_url['scheme']; |
||
| 65 | $ret['hostname'] = $parse_url['host']; |
||
| 66 | $ret['pathname'] = $parse_url['path'] ?? '/'; |
||
| 67 | |||
| 68 | if (isset($parse_url['query'])) { |
||
| 69 | $ret['pathname'] .= "?" . $parse_url['query']; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($parse_url['scheme'] == "https") { |
||
| 73 | $ret['sockAddress'] = "ssl://" . $ret['hostname']; |
||
| 74 | } else { |
||
| 75 | $ret['sockAddress'] = $ret['hostname']; |
||
| 76 | } |
||
| 77 | |||
| 78 | return $ret; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param resource $fp File handle |
||
| 83 | */ |
||
| 84 | private static function parseTitleFromStreamMetadata($fp) : ?string { |
||
| 85 | $meta_length = \ord(\fread($fp, 1)) * 16; |
||
| 86 | if ($meta_length) { |
||
| 87 | $metadatas = \explode(';', \fread($fp, $meta_length)); |
||
| 88 | $title = self::findStrFollowing($metadatas, "StreamTitle="); |
||
| 89 | if ($title) { |
||
| 90 | return StringUtil::truncate(\trim($title, "'"), 256); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | return null; |
||
| 94 | } |
||
| 95 | |||
| 96 | private function readMetadata(string $metaUrl, callable $parseResult) : ?array { |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | public function readShoutcastV1Metadata(string $streamUrl) : ?array { |
||
| 111 | // cut the URL from the last '/' and append 7.html |
||
| 112 | $lastSlash = \strrpos($streamUrl, '/'); |
||
| 113 | $metaUrl = \substr($streamUrl, 0, $lastSlash) . '/7.html'; |
||
| 114 | |||
| 115 | return $this->readMetadata($metaUrl, function ($content) { |
||
| 116 | // parsing logic borrowed from https://github.com/IntellexApps/shoutcast/blob/master/src/Info.php |
||
| 117 | |||
| 118 | // get rid of the <html><body>...</html></body> decorations and extra spacing: |
||
| 119 | $content = \preg_replace("[\n\t]", '', \trim(\strip_tags($content))); |
||
| 120 | |||
| 121 | // parse fields, allowing only the expected format |
||
| 122 | $match = []; |
||
| 123 | if (!\preg_match('~^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,(.*?)$~', $content, $match)) { |
||
| 124 | return null; |
||
| 125 | } else { |
||
| 126 | return [ |
||
| 127 | 'type' => 'shoutcast-v1', |
||
| 128 | 'title' => $match[7], |
||
| 129 | 'bitrate' => $match[6] |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | }); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function readShoutcastV2Metadata(string $streamUrl) : ?array { |
||
| 136 | // cut the URL from the last '/' and append 'stats' |
||
| 137 | $lastSlash = \strrpos($streamUrl, '/'); |
||
| 138 | $metaUrl = \substr($streamUrl, 0, $lastSlash) . '/stats'; |
||
| 139 | |||
| 140 | return $this->readMetadata($metaUrl, function ($content) { |
||
| 141 | $rootNode = \simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_NOCDATA); |
||
| 142 | if ($rootNode === false || $rootNode->getName() != 'SHOUTCASTSERVER') { |
||
| 143 | return null; |
||
| 144 | } else { |
||
| 145 | return [ |
||
| 146 | 'type' => 'shoutcast-v2', |
||
| 147 | 'title' => (string)$rootNode->SONGTITLE, |
||
| 148 | 'station' => (string)$rootNode->SERVERTITLE, |
||
| 149 | 'homepage' => (string)$rootNode->SERVERURL, |
||
| 150 | 'genre' => (string)$rootNode->SERVERGENRE, |
||
| 151 | 'bitrate' => (string)$rootNode->BITRATE |
||
| 152 | ]; |
||
| 153 | } |
||
| 154 | }); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function readIcecastMetadata(string $streamUrl) : ?array { |
||
| 158 | // cut the URL from the last '/' and append 'status-json.xsl' |
||
| 159 | $lastSlash = \strrpos($streamUrl, '/'); |
||
| 160 | $metaUrl = \substr($streamUrl, 0, $lastSlash) . '/status-json.xsl'; |
||
| 161 | |||
| 162 | return $this->readMetadata($metaUrl, function ($content) use ($streamUrl) { |
||
| 163 | \mb_substitute_character(0xFFFD); // Use the Unicode REPLACEMENT CHARACTER (U+FFFD) |
||
| 164 | $content = \mb_convert_encoding($content, 'UTF-8', 'UTF-8'); |
||
| 165 | $parsed = \json_decode(/** @scrutinizer ignore-type */ $content, true); |
||
| 166 | $source = $parsed['icestats']['source'] ?? null; |
||
| 167 | |||
| 168 | if (!\is_array($source)) { |
||
| 169 | return null; |
||
| 170 | } else { |
||
| 171 | // There may be one or multiple sources and the structure is slightly different in these two cases. |
||
| 172 | // In case there are multiple, try to found the source with a matching stream URL. |
||
| 173 | if (\is_int(\key($source))) { |
||
| 174 | // multiple sources |
||
| 175 | foreach ($source as $sourceItem) { |
||
| 176 | if ($sourceItem['listenurl'] == $streamUrl) { |
||
| 177 | $source = $sourceItem; |
||
| 178 | break; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | return [ |
||
| 184 | 'type' => 'icecast', |
||
| 185 | 'title' => $source['title'] ?? $source['yp_currently_playing'] ?? null, |
||
| 186 | 'station' => $source['server_name'] ?? null, |
||
| 187 | 'description' => $source['server_description'] ?? null, |
||
| 188 | 'homepage' => $source['server_url'] ?? null, |
||
| 189 | 'genre' => $source['genre'] ?? null, |
||
| 190 | 'bitrate' => $source['bitrate'] ?? null |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | }); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function readIcyMetadata(string $streamUrl, int $maxattempts, int $maxredirect) : ?array { |
||
| 261 | } |
||
| 262 | |||
| 263 | private static function convertUrlOnPlaylistToAbsolute(string $containedUrl, string $playlistUrl) : string { |
||
| 264 | if (!StringUtil::startsWith($containedUrl, 'http://', true) && !StringUtil::startsWith($containedUrl, 'https://', true)) { |
||
| 265 | $urlParts = \parse_url($playlistUrl); |
||
| 266 | $path = $urlParts['path']; |
||
| 267 | $lastSlash = \strrpos($path, '/'); |
||
| 268 | $urlParts['path'] = \substr($path, 0, $lastSlash + 1) . $containedUrl; |
||
| 269 | unset($urlParts['query'], $urlParts['fragment']); |
||
| 270 | |||
| 271 | $containedUrl = Util::buildUrl($urlParts); |
||
| 272 | } |
||
| 273 | return $containedUrl; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sometimes the URL given as stream URL points to a playlist which in turn contains the actual |
||
| 278 | * URL to be streamed. This function resolves such indirections. |
||
| 279 | * @return array{url: ?string, hls: bool} |
||
| 280 | */ |
||
| 281 | public function resolveStreamUrl(string $url) : array { |
||
| 328 | } |
||
| 329 | |||
| 330 | public function getHlsManifest(string $url) : array { |
||
| 331 | $maxLength = 8 * 1024; |
||
| 332 | $result = HttpUtil::loadFromUrl($url, $maxLength); |
||
| 366 |