4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-14 05:06:37 +00:00
AzuraCast/src/Entity/ApiGenerator/StationApiGenerator.php
2022-06-03 22:39:02 -05:00

78 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Entity\ApiGenerator;
use App\Entity;
use App\Http\Router;
use App\Radio\Adapters;
use Psr\Http\Message\UriInterface;
class StationApiGenerator
{
public function __construct(
protected Adapters $adapters,
protected Router $router
) {
}
public function __invoke(
Entity\Station $station,
?UriInterface $baseUri = null,
bool $showAllMounts = false
): Entity\Api\NowPlaying\Station {
$fa = $this->adapters->getFrontendAdapter($station);
$backend = $this->adapters->getBackendAdapter($station);
$remoteAdapters = $this->adapters->getRemoteAdapters($station);
$response = new Entity\Api\NowPlaying\Station();
$response->id = (int)$station->getId();
$response->name = (string)$station->getName();
$response->shortcode = $station->getShortName();
$response->description = (string)$station->getDescription();
$response->frontend = (string)$station->getFrontendType();
$response->backend = (string)$station->getBackendType();
$response->url = $station->getUrl();
$response->is_public = $station->getEnablePublicPage();
$response->listen_url = $fa->getStreamUrl($station, $baseUri);
$response->public_player_url = (string)$this->router->named(
'public:index',
['station_id' => $station->getShortName()]
);
$response->playlist_pls_url = (string)$this->router->named(
'public:playlist',
['station_id' => $station->getShortName(), 'format' => 'pls']
);
$response->playlist_m3u_url = (string)$this->router->named(
'public:playlist',
['station_id' => $station->getShortName(), 'format' => 'm3u']
);
$mounts = [];
if ($fa->supportsMounts() && $station->getMounts()->count() > 0) {
foreach ($station->getMounts() as $mount) {
if ($showAllMounts || $mount->getIsVisibleOnPublicPages()) {
$mounts[] = $mount->api($fa, $baseUri);
}
}
}
$response->mounts = $mounts;
$remotes = [];
foreach ($remoteAdapters as $ra_proxy) {
$remote = $ra_proxy->getRemote();
if ($showAllMounts || $remote->getIsVisibleOnPublicPages()) {
$remotes[] = $remote->api($ra_proxy->getAdapter());
}
}
$response->remotes = $remotes;
$response->hls_enabled = $backend->supportsHls() && $station->getEnableHls();
$response->hls_url = $backend->getHlsUrl($station, $baseUri);
return $response;
}
}