Create new expandable StationBrandingConfiguration object.

This commit is contained in:
Buster Neece 2023-01-02 14:47:16 -06:00
parent 5a292d4330
commit 24ef1c4d8a
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
5 changed files with 130 additions and 31 deletions

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Entity\Migration;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20230102192033 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create Station branding config column, part 1.';
}
public function up(Schema $schema): void
{
$this->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');
}
}

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Entity\Migration;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230102192652 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create Station branding config column, part 2.';
}
public function up(Schema $schema): void
{
$this->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');
}
}

View File

@ -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;
}

View File

@ -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<int, SongHistory> */
#[
@ -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();
}
/**

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Utilities\Urls;
use Psr\Http\Message\UriInterface;
class StationBrandingConfiguration extends AbstractStationConfiguration
{
public const DEFAULT_ALBUM_ART_URL = 'default_album_art_url';
public function getDefaultAlbumArtUrl(): ?string
{
return $this->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);
}
}