From 24ef1c4d8aacae7953c7523e6d700a702d6d635c Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Mon, 2 Jan 2023 14:47:16 -0600 Subject: [PATCH] Create new expandable StationBrandingConfiguration object. --- .../Migration/Version20230102192033.php | 51 +++++++++++++++++++ .../Migration/Version20230102192652.php | 29 +++++++++++ src/Entity/Repository/StationRepository.php | 2 +- src/Entity/Station.php | 47 +++++++---------- src/Entity/StationBrandingConfiguration.php | 32 ++++++++++++ 5 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 src/Entity/Migration/Version20230102192033.php create mode 100644 src/Entity/Migration/Version20230102192652.php create mode 100644 src/Entity/StationBrandingConfiguration.php diff --git a/src/Entity/Migration/Version20230102192033.php b/src/Entity/Migration/Version20230102192033.php new file mode 100644 index 000000000..fdb75adcd --- /dev/null +++ b/src/Entity/Migration/Version20230102192033.php @@ -0,0 +1,51 @@ +addSql('ALTER TABLE station ADD branding_config LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:json)\''); + } + + public function postUp(Schema $schema): void + { + $allStations = $this->connection->fetchAllAssociative( + <<<'SQL' + SELECT id, default_album_art_url + FROM station + WHERE default_album_art_url IS NOT NULL + SQL + ); + + foreach ($allStations as $station) { + $this->connection->update( + 'station', + [ + 'branding_config' => json_encode([ + 'default_album_art_url' => $station['default_album_art_url'], + ], JSON_THROW_ON_ERROR), + ], + [ + 'id' => $station['id'], + ] + ); + } + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE station DROP branding_config'); + } +} diff --git a/src/Entity/Migration/Version20230102192652.php b/src/Entity/Migration/Version20230102192652.php new file mode 100644 index 000000000..29147deaf --- /dev/null +++ b/src/Entity/Migration/Version20230102192652.php @@ -0,0 +1,29 @@ +addSql('ALTER TABLE station DROP default_album_art_url'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE station ADD default_album_art_url VARCHAR(255) DEFAULT NULL'); + } +} diff --git a/src/Entity/Repository/StationRepository.php b/src/Entity/Repository/StationRepository.php index 30ecc87e6..a3bd3ac87 100644 --- a/src/Entity/Repository/StationRepository.php +++ b/src/Entity/Repository/StationRepository.php @@ -181,7 +181,7 @@ final class StationRepository extends Repository public function getDefaultAlbumArtUrl(?Entity\Station $station = null): UriInterface { if (null !== $station) { - $stationCustomUri = $station->getDefaultAlbumArtUrlAsUri(); + $stationCustomUri = $station->getBrandingConfig()->getDefaultAlbumArtUrlAsUri(); if (null !== $stationCustomUri) { return $stationCustomUri; } diff --git a/src/Entity/Station.php b/src/Entity/Station.php index 52fd74f3e..917b812dd 100644 --- a/src/Entity/Station.php +++ b/src/Entity/Station.php @@ -13,7 +13,6 @@ use App\Normalizer\Attributes\DeepNormalize; use App\Radio\Enums\BackendAdapters; use App\Radio\Enums\FrontendAdapters; use App\Utilities\File; -use App\Utilities\Urls; use App\Validator\Constraints as AppAssert; use DateTimeZone; use Doctrine\Common\Collections\ArrayCollection; @@ -23,7 +22,6 @@ use InvalidArgumentException; use League\Flysystem\UnixVisibility\PortableVisibilityConverter; use League\Flysystem\Visibility; use OpenApi\Attributes as OA; -use Psr\Http\Message\UriInterface; use RuntimeException; use Stringable; use Symfony\Component\Filesystem\Filesystem; @@ -288,13 +286,14 @@ class Station implements Stringable, IdentifiableEntityInterface #[ OA\Property( - description: "The station-specific default album artwork URL.", - example: "https://example.com/image.jpg" + description: "An array containing station-specific branding configuration", + type: "array", + items: new OA\Items() ), - ORM\Column(length: 255, nullable: true), + ORM\Column(type: 'json', nullable: true), Serializer\Groups([EntityGroupsInterface::GROUP_GENERAL, EntityGroupsInterface::GROUP_ALL]) ] - protected ?string $default_album_art_url = null; + protected ?array $branding_config = null; /** @var Collection */ #[ @@ -492,10 +491,6 @@ class Station implements Stringable, IdentifiableEntityInterface return new StationFrontendConfiguration((array)$this->frontend_config); } - /** - * @param array|StationFrontendConfiguration $frontend_config - * @param bool $force_overwrite - */ public function setFrontendConfig( StationFrontendConfiguration|array $frontend_config, bool $force_overwrite = false @@ -560,10 +555,6 @@ class Station implements Stringable, IdentifiableEntityInterface ($this->getBackendTypeEnum()->isEnabled() || $this->getFrontendTypeEnum()->isEnabled()); } - /** - * @param array|StationBackendConfiguration $backend_config - * @param bool $force_overwrite - */ public function setBackendConfig( StationBackendConfiguration|array $backend_config, bool $force_overwrite = false @@ -930,26 +921,22 @@ class Station implements Stringable, IdentifiableEntityInterface $this->timezone = $timezone; } - public function getDefaultAlbumArtUrl(): ?string + public function getBrandingConfig(): StationBrandingConfiguration { - return $this->default_album_art_url; + return new StationBrandingConfiguration((array)$this->branding_config); } - public function getDefaultAlbumArtUrlAsUri(): ?UriInterface - { - return Urls::tryParseUserUrl( - $this->default_album_art_url, - 'Station ' . $this->__toString() . ' Default Album Art URL', - false - ); - } + public function setBrandingConfig( + StationBrandingConfiguration|array $brandingConfig, + bool $forceOverwrite = false + ): void { + if (is_array($brandingConfig)) { + $brandingConfig = new StationBrandingConfiguration( + $forceOverwrite ? $brandingConfig : array_merge((array)$this->branding_config, $brandingConfig) + ); + } - /** - * @param string|null $default_album_art_url - */ - public function setDefaultAlbumArtUrl(?string $default_album_art_url): void - { - $this->default_album_art_url = $default_album_art_url; + $this->branding_config = $brandingConfig->toArray(); } /** diff --git a/src/Entity/StationBrandingConfiguration.php b/src/Entity/StationBrandingConfiguration.php new file mode 100644 index 000000000..20ceb5345 --- /dev/null +++ b/src/Entity/StationBrandingConfiguration.php @@ -0,0 +1,32 @@ +get(self::DEFAULT_ALBUM_ART_URL); + } + + public function getDefaultAlbumArtUrlAsUri(): ?UriInterface + { + return Urls::tryParseUserUrl( + $this->getDefaultAlbumArtUrl(), + 'Station Default Album Art URL', + false + ); + } + + public function setDefaultAlbumArtUrl(?string $default_album_art_url): void + { + $this->set(self::DEFAULT_ALBUM_ART_URL, $default_album_art_url); + } +}