2017-01-17 09:46:43 +01:00
|
|
|
<?php
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Entity\Repository;
|
2017-01-17 09:46:43 +01:00
|
|
|
|
2020-03-29 09:16:41 +02:00
|
|
|
use App\Doctrine\Repository;
|
2019-08-07 06:33:55 +02:00
|
|
|
use App\Entity;
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Radio\Adapters;
|
|
|
|
use App\Radio\Configuration;
|
2018-12-27 09:24:07 +01:00
|
|
|
use App\Radio\Frontend\AbstractFrontend;
|
2020-07-08 09:03:50 +02:00
|
|
|
use App\Settings;
|
2018-12-05 08:15:51 +01:00
|
|
|
use App\Sync\Task\Media;
|
2019-09-04 20:00:51 +02:00
|
|
|
use App\Utilities;
|
|
|
|
use Closure;
|
2020-06-26 22:22:53 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-09-04 20:00:51 +02:00
|
|
|
use Exception;
|
2020-07-10 06:15:27 +02:00
|
|
|
use GuzzleHttp\Psr7\Uri;
|
|
|
|
use Psr\Http\Message\UriInterface;
|
2019-09-29 09:50:24 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-08-07 19:50:48 +02:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2019-09-29 09:50:24 +02:00
|
|
|
use Symfony\Component\Serializer\Serializer;
|
2019-04-09 11:06:44 +02:00
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
2017-01-17 09:46:43 +01:00
|
|
|
|
2018-11-12 17:59:15 +01:00
|
|
|
class StationRepository extends Repository
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2020-03-29 09:16:41 +02:00
|
|
|
protected Media $media_sync;
|
2018-12-05 08:15:51 +01:00
|
|
|
|
2020-03-29 09:16:41 +02:00
|
|
|
protected Adapters $adapters;
|
2018-12-05 08:15:51 +01:00
|
|
|
|
2020-03-29 09:16:41 +02:00
|
|
|
protected Configuration $configuration;
|
2018-12-05 08:15:51 +01:00
|
|
|
|
2020-03-29 09:16:41 +02:00
|
|
|
protected ValidatorInterface $validator;
|
2019-04-09 11:06:44 +02:00
|
|
|
|
2020-03-29 09:16:41 +02:00
|
|
|
protected CacheInterface $cache;
|
2019-01-26 21:38:36 +01:00
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
protected SettingsRepository $settingsRepo;
|
|
|
|
|
2019-09-29 09:50:24 +02:00
|
|
|
public function __construct(
|
2020-06-26 22:22:53 +02:00
|
|
|
EntityManagerInterface $em,
|
2019-09-29 09:50:24 +02:00
|
|
|
Serializer $serializer,
|
2020-07-08 09:03:50 +02:00
|
|
|
Settings $settings,
|
2020-07-10 06:15:27 +02:00
|
|
|
SettingsRepository $settingsRepo,
|
2019-09-29 09:50:24 +02:00
|
|
|
LoggerInterface $logger,
|
|
|
|
Media $media_sync,
|
|
|
|
Adapters $adapters,
|
|
|
|
Configuration $configuration,
|
|
|
|
ValidatorInterface $validator,
|
|
|
|
CacheInterface $cache
|
|
|
|
) {
|
|
|
|
$this->media_sync = $media_sync;
|
|
|
|
$this->adapters = $adapters;
|
|
|
|
$this->configuration = $configuration;
|
|
|
|
$this->validator = $validator;
|
|
|
|
$this->cache = $cache;
|
2020-07-10 06:15:27 +02:00
|
|
|
$this->settingsRepo = $settingsRepo;
|
2019-09-29 09:50:24 +02:00
|
|
|
|
|
|
|
parent::__construct($em, $serializer, $settings, $logger);
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:31:13 +02:00
|
|
|
/**
|
|
|
|
* @param string $identifier A numeric or string identifier for a station.
|
2019-09-20 18:44:38 +02:00
|
|
|
*
|
2019-09-19 04:31:13 +02:00
|
|
|
* @return Entity\Station|null
|
|
|
|
*/
|
|
|
|
public function findByIdentifier(string $identifier): ?Entity\Station
|
|
|
|
{
|
|
|
|
return is_numeric($identifier)
|
2019-09-29 09:50:24 +02:00
|
|
|
? $this->repository->find($identifier)
|
|
|
|
: $this->repository->findOneBy(['short_name' => $identifier]);
|
2019-09-19 04:31:13 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fetchAll()
|
|
|
|
{
|
2019-09-29 09:50:24 +02:00
|
|
|
return $this->em->createQuery(/** @lang DQL */ 'SELECT s FROM App\Entity\Station s ORDER BY s.name ASC')
|
2017-01-17 09:46:43 +01:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-01 00:32:30 +02:00
|
|
|
* @param bool|string $add_blank
|
2019-09-04 20:00:51 +02:00
|
|
|
* @param Closure|NULL $display
|
2017-01-17 09:46:43 +01:00
|
|
|
* @param string $pk
|
|
|
|
* @param string $order_by
|
2019-09-20 18:44:38 +02:00
|
|
|
*
|
2017-01-17 09:46:43 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-09-29 09:50:24 +02:00
|
|
|
public function fetchSelect($add_blank = false, Closure $display = null, $pk = 'id', $order_by = 'name'): array
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2017-01-24 01:35:16 +01:00
|
|
|
$select = [];
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Specify custom text in the $add_blank parameter to override.
|
2017-01-24 01:35:16 +01:00
|
|
|
if ($add_blank !== false) {
|
|
|
|
$select[''] = ($add_blank === true) ? 'Select...' : $add_blank;
|
|
|
|
}
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Build query for records.
|
|
|
|
$results = $this->fetchArray();
|
|
|
|
|
|
|
|
// Assemble select values and, if necessary, call $display callback.
|
2019-01-31 18:54:17 +01:00
|
|
|
foreach ($results as $result) {
|
2017-01-17 09:46:43 +01:00
|
|
|
$key = $result[$pk];
|
2017-01-24 01:35:16 +01:00
|
|
|
$value = ($display === null) ? $result['name'] : $display($result);
|
2017-01-17 09:46:43 +01:00
|
|
|
$select[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $select;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-31 18:54:17 +01:00
|
|
|
* @param string $short_code
|
2019-09-20 18:44:38 +02:00
|
|
|
*
|
2017-01-17 09:46:43 +01:00
|
|
|
* @return null|object
|
|
|
|
*/
|
|
|
|
public function findByShortCode($short_code)
|
|
|
|
{
|
2019-09-29 09:50:24 +02:00
|
|
|
return $this->repository->findOneBy(['short_name' => $short_code]);
|
2017-01-17 09:46:43 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 21:38:36 +01:00
|
|
|
/**
|
|
|
|
* @param Entity\Station $record
|
2020-01-30 02:21:15 +01:00
|
|
|
*
|
|
|
|
* @return Entity\Station
|
2019-01-26 21:38:36 +01:00
|
|
|
*/
|
2020-01-30 02:21:15 +01:00
|
|
|
public function edit(Entity\Station $record): Entity\Station
|
2019-01-26 21:38:36 +01:00
|
|
|
{
|
2019-09-29 09:50:24 +02:00
|
|
|
$original_record = $this->em->getUnitOfWork()->getOriginalEntityData($record);
|
2019-01-26 21:38:36 +01:00
|
|
|
|
2019-04-09 11:06:44 +02:00
|
|
|
// Get the original values to check for changes.
|
|
|
|
$old_frontend = $original_record['frontend_type'];
|
|
|
|
$old_backend = $original_record['backend_type'];
|
2019-01-26 21:38:36 +01:00
|
|
|
|
|
|
|
$frontend_changed = ($old_frontend !== $record->getFrontendType());
|
|
|
|
$backend_changed = ($old_backend !== $record->getBackendType());
|
|
|
|
$adapter_changed = $frontend_changed || $backend_changed;
|
|
|
|
|
|
|
|
if ($frontend_changed) {
|
|
|
|
$frontend = $this->adapters->getFrontendAdapter($record);
|
|
|
|
$this->resetMounts($record, $frontend);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->configuration->writeConfiguration($record, $adapter_changed);
|
|
|
|
|
2019-08-07 19:50:48 +02:00
|
|
|
$this->cache->delete('stations');
|
2020-01-30 02:21:15 +01:00
|
|
|
|
|
|
|
return $record;
|
2019-01-26 21:38:36 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 20:00:51 +02:00
|
|
|
/**
|
|
|
|
* Reset mount points to their adapter defaults (in the event of an adapter change).
|
|
|
|
*
|
|
|
|
* @param Entity\Station $station
|
|
|
|
* @param AbstractFrontend $frontend_adapter
|
|
|
|
*/
|
|
|
|
public function resetMounts(Entity\Station $station, AbstractFrontend $frontend_adapter): void
|
|
|
|
{
|
|
|
|
foreach ($station->getMounts() as $mount) {
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->remove($mount);
|
2019-09-04 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create default mountpoints if station supports them.
|
|
|
|
if ($frontend_adapter::supportsMounts()) {
|
|
|
|
// Create default mount points.
|
|
|
|
$mount_points = $frontend_adapter::getDefaultMounts();
|
|
|
|
|
|
|
|
foreach ($mount_points as $mount_point) {
|
|
|
|
$mount_record = new Entity\StationMount($station);
|
|
|
|
$this->fromArray($mount_record, $mount_point);
|
|
|
|
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->persist($mount_record);
|
2019-09-04 20:00:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->flush();
|
2019-09-04 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 09:46:43 +01:00
|
|
|
/**
|
2019-04-09 11:06:44 +02:00
|
|
|
* Handle tasks necessary to a station's creation.
|
2017-01-17 09:46:43 +01:00
|
|
|
*
|
2019-04-09 11:06:44 +02:00
|
|
|
* @param Entity\Station $station
|
2020-01-30 02:21:15 +01:00
|
|
|
*
|
|
|
|
* @return Entity\Station
|
2017-01-17 09:46:43 +01:00
|
|
|
*/
|
2020-01-30 02:21:15 +01:00
|
|
|
public function create(Entity\Station $station): Entity\Station
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2017-05-10 11:27:22 +02:00
|
|
|
// Create path for station.
|
2020-03-29 09:29:29 +02:00
|
|
|
$station->setRadioBaseDir(null);
|
2017-05-10 11:27:22 +02:00
|
|
|
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->persist($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Generate station ID.
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->flush();
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Scan directory for any existing files.
|
|
|
|
set_time_limit(600);
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->media_sync->importMusic($station);
|
2020-01-30 01:22:21 +01:00
|
|
|
|
2020-01-30 02:21:15 +01:00
|
|
|
/** @var Entity\Station $station */
|
2020-01-30 01:22:21 +01:00
|
|
|
$station = $this->em->find(Entity\Station::class, $station->getId());
|
2017-01-17 09:46:43 +01:00
|
|
|
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->media_sync->importPlaylists($station);
|
2020-01-30 01:22:21 +01:00
|
|
|
|
2020-01-30 02:21:15 +01:00
|
|
|
/** @var Entity\Station $station */
|
2020-01-30 01:22:21 +01:00
|
|
|
$station = $this->em->find(Entity\Station::class, $station->getId());
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Load adapters.
|
2018-12-05 08:15:51 +01:00
|
|
|
$frontend_adapter = $this->adapters->getFrontendAdapter($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Create default mountpoints if station supports them.
|
2018-02-01 12:49:40 +01:00
|
|
|
$this->resetMounts($station, $frontend_adapter);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Load configuration from adapter to pull source and admin PWs.
|
2018-09-22 13:52:43 +02:00
|
|
|
$frontend_adapter->read($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Write the adapter configurations and update supervisord.
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->configuration->writeConfiguration($station, true);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Save changes and continue to the last setup step.
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->persist($station);
|
|
|
|
$this->em->flush();
|
2017-01-17 09:46:43 +01:00
|
|
|
|
2019-08-07 20:03:18 +02:00
|
|
|
$this->cache->delete('stations');
|
2020-01-30 02:21:15 +01:00
|
|
|
|
|
|
|
return $station;
|
2017-01-17 09:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Entity\Station $station
|
2019-09-20 18:44:38 +02:00
|
|
|
*
|
2019-09-04 20:00:51 +02:00
|
|
|
* @throws Exception
|
2017-01-17 09:46:43 +01:00
|
|
|
*/
|
2019-01-26 21:38:36 +01:00
|
|
|
public function destroy(Entity\Station $station): void
|
2017-01-17 09:46:43 +01:00
|
|
|
{
|
2018-12-05 08:15:51 +01:00
|
|
|
$this->configuration->removeConfiguration($station);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Remove media folders.
|
|
|
|
$radio_dir = $station->getRadioBaseDir();
|
2019-09-04 20:00:51 +02:00
|
|
|
Utilities::rmdirRecursive($radio_dir);
|
2017-01-17 09:46:43 +01:00
|
|
|
|
|
|
|
// Save changes and continue to the last setup step.
|
2020-06-25 20:44:26 +02:00
|
|
|
$this->em->flush();
|
2019-09-29 09:50:24 +02:00
|
|
|
$this->em->remove($station);
|
2020-06-25 20:44:26 +02:00
|
|
|
$this->em->flush();
|
2019-01-26 21:38:36 +01:00
|
|
|
|
2019-08-07 20:03:18 +02:00
|
|
|
$this->cache->delete('stations');
|
2019-01-26 21:38:36 +01:00
|
|
|
}
|
2020-04-01 08:01:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the now-playing cache from all stations.
|
|
|
|
*/
|
|
|
|
public function clearNowPlaying(): void
|
|
|
|
{
|
|
|
|
$this->em->createQuery(/** @lang DQL */ 'UPDATE App\Entity\Station s SET s.nowplaying=null')
|
|
|
|
->execute();
|
|
|
|
}
|
2020-07-10 06:15:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the URL to use for songs with no specified album artwork, when artwork is displayed.
|
|
|
|
*
|
|
|
|
* @param Entity\Station|null $station
|
|
|
|
*
|
|
|
|
* @return UriInterface
|
|
|
|
*/
|
|
|
|
public function getDefaultAlbumArtUrl(?Entity\Station $station = null): UriInterface
|
|
|
|
{
|
|
|
|
if ($station instanceof Entity\Station) {
|
|
|
|
$stationCustomUrl = trim($station->getDefaultAlbumArtUrl());
|
|
|
|
|
|
|
|
if (!empty($stationCustomUrl)) {
|
|
|
|
return new Uri($stationCustomUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$custom_url = trim($this->settingsRepo->getSetting(Entity\Settings::DEFAULT_ALBUM_ART_URL));
|
|
|
|
|
|
|
|
if (!empty($custom_url)) {
|
|
|
|
return new Uri($custom_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Uri('/static/img/generic_song.jpg');
|
|
|
|
}
|
2018-08-05 00:05:14 +02:00
|
|
|
}
|