Make entity functions line up with propertyaccess.

This commit is contained in:
Buster "Silver Eagle" Neece 2021-11-06 23:02:44 -06:00
parent cbe106d9ef
commit 058f306c15
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
29 changed files with 49 additions and 139 deletions

View File

@ -159,7 +159,7 @@ class RelaysController
$remote->setRelay($relay); $remote->setRelay($relay);
$remote->setType(Adapters::REMOTE_AZURARELAY); $remote->setType(Adapters::REMOTE_AZURARELAY);
$remote->setDisplayName($mount->getDisplayName() . ' (' . $relay->getName() . ')'); $remote->setDisplayName($mount->getDisplayName() . ' (' . $relay->getName() . ')');
$remote->setIsVisibleOnPublicPages($relay->isIsVisibleOnPublicPages()); $remote->setIsVisibleOnPublicPages($relay->getIsVisibleOnPublicPages());
$remote->setAutodjBitrate($mount->getAutodjBitrate()); $remote->setAutodjBitrate($mount->getAutodjBitrate());
$remote->setAutodjFormat($mount->getAutodjFormat()); $remote->setAutodjFormat($mount->getAutodjFormat());
$remote->setUrl($relay->getBaseUrl()); $remote->setUrl($relay->getBaseUrl());

View File

@ -175,6 +175,7 @@ class StationsController extends AbstractAdminApiCrudController
if (!empty($data[$idKey])) { if (!empty($data[$idKey])) {
$data[$locationKey] = $data[$idKey]; $data[$locationKey] = $data[$idKey];
} }
unset($data[$idKey]);
} }
return parent::fromArray($data, $record, $context); return parent::fromArray($data, $record, $context);

View File

@ -38,7 +38,7 @@ class ChartsAction
$em->getRepository(Entity\Station::class)->findAll(), $em->getRepository(Entity\Station::class)->findAll(),
static function ($station) use ($acl) { static function ($station) use ($acl) {
/** @var Entity\Station $station */ /** @var Entity\Station $station */
return $station->isEnabled() && return $station->getIsEnabled() &&
$acl->isAllowed(Acl::STATION_VIEW, $station->getId()); $acl->isAllowed(Acl::STATION_VIEW, $station->getId());
} }
); );

View File

@ -28,7 +28,7 @@ class StationsAction
$em->getRepository(Entity\Station::class)->findAll(), $em->getRepository(Entity\Station::class)->findAll(),
static function ($station) use ($acl) { static function ($station) use ($acl) {
/** @var Entity\Station $station */ /** @var Entity\Station $station */
return $station->isEnabled() && return $station->getIsEnabled() &&
$acl->isAllowed(Acl::STATION_VIEW, $station->getId()); $acl->isAllowed(Acl::STATION_VIEW, $station->getId());
} }
); );

View File

@ -15,7 +15,8 @@ class ToggleAction extends AbstractWebhooksAction
{ {
$record = $this->requireRecord($request->getStation(), $id); $record = $this->requireRecord($request->getStation(), $id);
$newValue = $record->toggleEnabled(); $newValue = !$record->getIsEnabled();
$record->setIsEnabled($newValue);
$this->em->persist($record); $this->em->persist($record);
$this->em->flush(); $this->em->flush();

View File

@ -24,7 +24,7 @@ class PlaylistAction
$fa = $request->getStationFrontend(); $fa = $request->getStationFrontend();
foreach ($station->getMounts() as $mount) { foreach ($station->getMounts() as $mount) {
/** @var Entity\StationMount $mount */ /** @var Entity\StationMount $mount */
if (!$mount->isVisibleOnPublicPages()) { if (!$mount->getIsVisibleOnPublicPages()) {
continue; continue;
} }
@ -41,7 +41,7 @@ class PlaylistAction
$adapter = $remote_proxy->getAdapter(); $adapter = $remote_proxy->getAdapter();
$remote = $remote_proxy->getRemote(); $remote = $remote_proxy->getRemote();
if (!$remote->isVisibleOnPublicPages()) { if (!$remote->getIsVisibleOnPublicPages()) {
continue; continue;
} }

View File

@ -29,7 +29,7 @@ class ProfileController
$station = $request->getStation(); $station = $request->getStation();
$view = $request->getView(); $view = $request->getView();
if (!$station->isEnabled()) { if (!$station->getIsEnabled()) {
return $view->renderToResponse($response, 'stations/profile/disabled'); return $view->renderToResponse($response, 'stations/profile/disabled');
} }

View File

@ -52,7 +52,7 @@ class StationApiGenerator
$mounts = []; $mounts = [];
if ($fa->supportsMounts() && $station->getMounts()->count() > 0) { if ($fa->supportsMounts() && $station->getMounts()->count() > 0) {
foreach ($station->getMounts() as $mount) { foreach ($station->getMounts() as $mount) {
if ($showAllMounts || $mount->isVisibleOnPublicPages()) { if ($showAllMounts || $mount->getIsVisibleOnPublicPages()) {
$mounts[] = $mount->api($fa, $baseUri); $mounts[] = $mount->api($fa, $baseUri);
} }
} }
@ -62,7 +62,7 @@ class StationApiGenerator
$remotes = []; $remotes = [];
foreach ($remoteAdapters as $ra_proxy) { foreach ($remoteAdapters as $ra_proxy) {
$remote = $ra_proxy->getRemote(); $remote = $ra_proxy->getRemote();
if ($showAllMounts || $remote->isVisibleOnPublicPages()) { if ($showAllMounts || $remote->getIsVisibleOnPublicPages()) {
$remotes[] = $remote->api($ra_proxy->getAdapter()); $remotes[] = $remote->api($ra_proxy->getAdapter());
} }
} }

View File

@ -78,7 +78,7 @@ class Relay implements IdentifiableEntityInterface
$this->name = $this->truncateNullableString($name, 100); $this->name = $this->truncateNullableString($name, 100);
} }
public function isIsVisibleOnPublicPages(): bool public function getIsVisibleOnPublicPages(): bool
{ {
return $this->is_visible_on_public_pages; return $this->is_visible_on_public_pages;
} }

View File

@ -286,11 +286,7 @@ class StationPlaylistMediaRepository extends Repository
->getQuery() ->getQuery()
->getSingleScalarResult(); ->getSingleScalarResult();
if ($notQueuedMediaCount === 0) { return $notQueuedMediaCount === 0;
return true;
}
return false;
} }
public function isQueueEmpty(Entity\StationPlaylist $playlist): bool public function isQueueEmpty(Entity\StationPlaylist $playlist): bool
@ -312,11 +308,7 @@ class StationPlaylistMediaRepository extends Repository
->getQuery() ->getQuery()
->getSingleScalarResult(); ->getSingleScalarResult();
if ($notQueuedMediaCount === $totalMediaCount) { return $notQueuedMediaCount === $totalMediaCount;
return true;
}
return false;
} }
protected function getCountPlaylistMediaBaseQuery(Entity\StationPlaylist $playlist): QueryBuilder protected function getCountPlaylistMediaBaseQuery(Entity\StationPlaylist $playlist): QueryBuilder

View File

@ -42,7 +42,7 @@ class StationQueueRepository extends Repository
public function newRecordSentToAutoDj(Entity\StationQueue $queueRow): void public function newRecordSentToAutoDj(Entity\StationQueue $queueRow): void
{ {
$queueRow->sentToAutoDj(); $queueRow->setSentToAutodj();
$this->em->persist($queueRow); $this->em->persist($queueRow);
$this->em->flush(); $this->em->flush();
} }

View File

@ -267,7 +267,7 @@ class SongHistory implements SongInterface, IdentifiableEntityInterface
public function showInApis(): bool public function showInApis(): bool
{ {
if ($this->playlist instanceof StationPlaylist) { if ($this->playlist instanceof StationPlaylist) {
return !$this->playlist->isJingle(); return !$this->playlist->getIsJingle();
} }
return true; return true;
} }

View File

@ -771,7 +771,7 @@ class Station implements Stringable, IdentifiableEntityInterface
public function getEnablePublicPage(): bool public function getEnablePublicPage(): bool
{ {
return $this->enable_public_page && $this->isEnabled(); return $this->enable_public_page && $this->getIsEnabled();
} }
public function setEnablePublicPage(bool $enable_public_page): void public function setEnablePublicPage(bool $enable_public_page): void
@ -799,7 +799,7 @@ class Station implements Stringable, IdentifiableEntityInterface
$this->enable_on_demand_download = $enable_on_demand_download; $this->enable_on_demand_download = $enable_on_demand_download;
} }
public function isEnabled(): bool public function getIsEnabled(): bool
{ {
return $this->is_enabled; return $this->is_enabled;
} }

View File

@ -159,7 +159,7 @@ class StationMount implements
$this->display_name = $this->truncateNullableString($display_name); $this->display_name = $this->truncateNullableString($display_name);
} }
public function isVisibleOnPublicPages(): bool public function getIsVisibleOnPublicPages(): bool
{ {
return $this->is_visible_on_public_pages; return $this->is_visible_on_public_pages;
} }

View File

@ -287,7 +287,7 @@ class StationPlaylist implements
$this->is_enabled = $is_enabled; $this->is_enabled = $is_enabled;
} }
public function isJingle(): bool public function getIsJingle(): bool
{ {
return $this->is_jingle; return $this->is_jingle;
} }

View File

@ -136,15 +136,17 @@ class StationQueue implements SongInterface, IdentifiableEntityInterface
$this->duration = $duration; $this->duration = $duration;
} }
public function isSentToAutoDj(): bool public function getSentToAutodj(): bool
{ {
return $this->sent_to_autodj; return $this->sent_to_autodj;
} }
public function sentToAutoDj(): void public function setSentToAutodj(bool $newValue = true): void
{ {
$this->setTimestampCued(time()); if ($newValue) {
$this->sent_to_autodj = true; $this->setTimestampCued(time());
}
$this->sent_to_autodj = $newValue;
} }
/** /**
@ -166,7 +168,7 @@ class StationQueue implements SongInterface, IdentifiableEntityInterface
public function showInApis(): bool public function showInApis(): bool
{ {
if ($this->playlist instanceof StationPlaylist) { if ($this->playlist instanceof StationPlaylist) {
return !$this->playlist->isJingle(); return !$this->playlist->getIsJingle();
} }
return true; return true;
} }

View File

@ -120,7 +120,7 @@ class StationRemote implements
$this->relay = $relay; $this->relay = $relay;
} }
public function isVisibleOnPublicPages(): bool public function getIsVisibleOnPublicPages(): bool
{ {
return $this->is_visible_on_public_pages; return $this->is_visible_on_public_pages;
} }

View File

@ -154,7 +154,7 @@ class StationStreamer implements
$this->comments = $comments; $this->comments = $comments;
} }
public function isActive(): bool public function getIsActive(): bool
{ {
return $this->is_active; return $this->is_active;
} }

View File

@ -128,17 +128,11 @@ class StationWebhook implements
return $this->type; return $this->type;
} }
public function isEnabled(): bool public function getIsEnabled(): bool
{ {
return $this->is_enabled; return $this->is_enabled;
} }
public function toggleEnabled(): bool
{
$this->is_enabled = !$this->is_enabled;
return $this->is_enabled;
}
public function setIsEnabled(bool $is_enabled): void public function setIsEnabled(bool $is_enabled): void
{ {
$this->is_enabled = $is_enabled; $this->is_enabled = $is_enabled;

View File

@ -1,7 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Normalizer; namespace App\Normalizer;
use App\Exception\NoGetterAvailableException; use App\Exception\NoGetterAvailableException;
@ -10,12 +8,9 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\Inflector\Inflector; use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory; use Doctrine\Inflector\InflectorFactory;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Proxy; use Doctrine\Persistence\Proxy;
use ProxyManager\Proxy\GhostObjectInterface; use ProxyManager\Proxy\GhostObjectInterface;
use ReflectionClass; use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionProperty; use ReflectionProperty;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
@ -356,85 +351,14 @@ class DoctrineEntityNormalizer extends AbstractNormalizer implements NormalizerA
} }
} }
/** protected function setProperty(object $entity, string $key, mixed $value): void
* @param object $entity
* @param string $key
* @param mixed $value
*
*/
protected function setProperty(object $entity, string $key, mixed $value): mixed
{ {
$method_name = $this->getMethodName($key, 'set'); $method_name = $this->getMethodName($key, 'set');
if (!method_exists($entity, $method_name)) { if (!method_exists($entity, $method_name)) {
return null; return;
} }
$method = new ReflectionMethod(get_class($entity), $method_name); $entity->$method_name($value);
$firstParam = $method->getParameters()[0];
$value = $this->castValue($firstParam, $value);
return $entity->$method_name($value);
}
protected function castValue(ReflectionParameter $firstParam, mixed $value): mixed
{
if (!$firstParam->hasType()) {
return $value;
}
$firstParamTypeObj = $firstParam->getType();
if (
!($firstParamTypeObj instanceof ReflectionNamedType)
|| !$firstParamTypeObj->isBuiltin()
) {
return $value;
}
switch ($firstParamTypeObj->getName()) {
case 'string':
if ($value === null) {
if (!$firstParam->allowsNull()) {
return '';
}
} else {
return (string)$value;
}
break;
case 'int':
if ($value === null) {
if (!$firstParam->allowsNull()) {
return 0;
}
} else {
return (int)$value;
}
break;
case 'float':
if ($value === null) {
if (!$firstParam->allowsNull()) {
return 0.0;
}
} else {
return (float)$value;
}
break;
case 'bool':
if ($value === null) {
if (!$firstParam->allowsNull()) {
return false;
}
} else {
return (bool)$value;
}
break;
}
return $value;
} }
protected function isEntity(mixed $class): bool protected function isEntity(mixed $class): bool
@ -445,11 +369,7 @@ class DoctrineEntityNormalizer extends AbstractNormalizer implements NormalizerA
: get_class($class); : get_class($class);
} }
if (!is_string($class)) { if (!is_string($class) || !class_exists($class)) {
return false;
}
if (!class_exists($class)) {
return false; return false;
} }

View File

@ -121,7 +121,7 @@ abstract class AbstractAdapter
*/ */
public function hasCommand(Entity\Station $station): bool public function hasCommand(Entity\Station $station): bool
{ {
if ($this->environment->isTesting() || !$station->isEnabled()) { if ($this->environment->isTesting() || !$station->getIsEnabled()) {
return false; return false;
} }

View File

@ -64,7 +64,7 @@ class Annotations implements EventSubscriberInterface
$playlist = $event->getPlaylist(); $playlist = $event->getPlaylist();
if ($playlist instanceof Entity\StationPlaylist) { if ($playlist instanceof Entity\StationPlaylist) {
// Handle "Jingle mode" by sending the same metadata as the previous song. // Handle "Jingle mode" by sending the same metadata as the previous song.
if ($playlist->isJingle()) { if ($playlist->getIsJingle()) {
$event->addAnnotations([ $event->addAnnotations([
'jingle_mode' => 'true', 'jingle_mode' => 'true',
]); ]);
@ -73,8 +73,8 @@ class Annotations implements EventSubscriberInterface
if ($np instanceof Entity\Api\NowPlaying\NowPlaying) { if ($np instanceof Entity\Api\NowPlaying\NowPlaying) {
$event->addAnnotations( $event->addAnnotations(
[ [
'title' => $np->now_playing?->song?->title, 'title' => $np->now_playing?->song?->title,
'artist' => $np->now_playing?->song?->artist, 'artist' => $np->now_playing?->song?->artist,
'playlist_id' => null, 'playlist_id' => null,
'media_id' => null, 'media_id' => null,
] ]

View File

@ -304,7 +304,7 @@ class ConfigWriter implements EventSubscriberInterface
$playlistConfigLines[] = $playlistVarName . ' = cue_cut(id="cue_' $playlistConfigLines[] = $playlistVarName . ' = cue_cut(id="cue_'
. self::cleanUpString($playlistVarName) . '", ' . $playlistVarName . ')'; . self::cleanUpString($playlistVarName) . '", ' . $playlistVarName . ')';
if ($playlist->isJingle()) { if ($playlist->getIsJingle()) {
$playlistConfigLines[] = $playlistVarName . ' = drop_metadata(' . $playlistVarName . ')'; $playlistConfigLines[] = $playlistVarName . ' = drop_metadata(' . $playlistVarName . ')';
} }
@ -569,7 +569,7 @@ class ConfigWriter implements EventSubscriberInterface
$mediaFilePath = $mediaBaseDir . $mediaFile->getPath(); $mediaFilePath = $mediaBaseDir . $mediaFile->getPath();
$mediaAnnotations = $this->liquidsoap->annotateMedia($mediaFile); $mediaAnnotations = $this->liquidsoap->annotateMedia($mediaFile);
if ($playlist->isJingle()) { if ($playlist->getIsJingle()) {
$mediaAnnotations['is_jingle_mode'] = 'true'; $mediaAnnotations['is_jingle_mode'] = 'true';
unset($mediaAnnotations['media_id']); unset($mediaAnnotations['media_id']);
} else { } else {

View File

@ -86,7 +86,7 @@ class Configuration
$supervisorConfig = []; $supervisorConfig = [];
$supervisorConfigFile = $this->getSupervisorConfigFile($station); $supervisorConfigFile = $this->getSupervisorConfigFile($station);
if (!$station->isEnabled()) { if (!$station->getIsEnabled()) {
@unlink($supervisorConfigFile); @unlink($supervisorConfigFile);
$this->reloadSupervisorForStation($station); $this->reloadSupervisorForStation($station);
return; return;

View File

@ -179,7 +179,7 @@ class Icecast extends AbstractFrontend
$mount['genre'] = $station->getGenre(); $mount['genre'] = $station->getGenre();
} }
if (!$mount_row->isVisibleOnPublicPages()) { if (!$mount_row->getIsVisibleOnPublicPages()) {
$mount['hidden'] = 1; $mount['hidden'] = 1;
} }

View File

@ -66,7 +66,7 @@ class CheckRequests extends AbstractTask
// Log the item in SongHistory. // Log the item in SongHistory.
$sq = Entity\StationQueue::fromRequest($request); $sq = Entity\StationQueue::fromRequest($request);
$sq->setTimestampCued(time()); $sq->setTimestampCued(time());
$sq->sentToAutodj(); $sq->setSentToAutodj();
$this->em->persist($sq); $this->em->persist($sq);
$this->em->flush(); $this->em->flush();

View File

@ -82,7 +82,7 @@ class NowPlayingTask extends AbstractTask implements EventSubscriberInterface
$nowplaying = []; $nowplaying = [];
foreach ($this->iterateStations() as $station) { foreach ($this->iterateStations() as $station) {
if (!$station->isEnabled()) { if (!$station->getIsEnabled()) {
continue; continue;
} }
@ -220,7 +220,7 @@ class NowPlayingTask extends AbstractTask implements EventSubscriberInterface
} }
} }
$sq->sentToAutodj(); $sq->setSentToAutodj();
$this->em->persist($sq); $this->em->persist($sq);
$this->em->flush(); $this->em->flush();

View File

@ -57,7 +57,7 @@ class Dispatcher
/** @var Entity\StationWebhook[] $enabledWebhooks */ /** @var Entity\StationWebhook[] $enabledWebhooks */
$enabledWebhooks = $station->getWebhooks()->filter( $enabledWebhooks = $station->getWebhooks()->filter(
function (Entity\StationWebhook $webhook) { function (Entity\StationWebhook $webhook) {
return $webhook->isEnabled(); return $webhook->getIsEnabled();
} }
); );

View File

@ -14,11 +14,11 @@ class Api_Admin_StationsCest extends CestAbstract
$I, $I,
'/api/admin/stations', '/api/admin/stations',
[ [
'name' => 'Test Station', 'name' => 'Test Station',
'short_name' => 'test_station', 'short_name' => 'test_station',
], ],
[ [
'name' => 'Modified Station', 'name' => 'Modified Station',
'frontend_type' => \App\Radio\Adapters::FRONTEND_SHOUTCAST, 'frontend_type' => \App\Radio\Adapters::FRONTEND_SHOUTCAST,
] ]
); );