Files
tonehaus/src/Entity/Album.php
boris 054e970df9
All checks were successful
CI - Build Tonehaus Docker image / tonehaus-ci-build (push) Successful in 1m55s
what the fuck
2025-11-27 20:03:12 +00:00

330 lines
7.8 KiB
PHP

<?php
namespace App\Entity;
use App\Repository\AlbumRepository;
use App\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Album aggregates Spotify or user-submitted metadata persisted in the catalog.
*/
#[ORM\Entity(repositoryClass: AlbumRepository::class)]
#[ORM\Table(name: 'albums')]
#[ORM\HasLifecycleCallbacks]
class Album
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
// For Spotify-sourced albums; null for user-created
#[ORM\Column(type: 'string', length: 64, unique: true, nullable: true)]
private ?string $spotifyId = null;
// Public identifier for user-created albums (e.g., "u_abc123"); null for Spotify
#[ORM\Column(type: 'string', length: 64, unique: true, nullable: true)]
private ?string $localId = null;
// 'spotify' or 'user'
#[ORM\Column(type: 'string', length: 16)]
private string $source = 'spotify';
#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
private string $name = '';
/**
* @var list<string>
*/
#[ORM\Column(type: 'json')]
private array $artists = [];
// Stored as given by Spotify: YYYY or YYYY-MM or YYYY-MM-DD
#[ORM\Column(type: 'string', length: 20, nullable: true)]
private ?string $releaseDate = null;
#[ORM\Column(type: 'integer')]
private int $totalTracks = 0;
#[ORM\Column(type: 'string', length: 1024, nullable: true)]
private ?string $coverUrl = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $coverImagePath = null;
#[ORM\Column(type: 'string', length: 1024, nullable: true)]
private ?string $externalUrl = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?User $createdBy = null;
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $updatedAt = null;
/**
* Initializes timestamps right before first persistence.
*/
#[ORM\PrePersist]
public function onPrePersist(): void
{
$now = new \DateTimeImmutable();
$this->createdAt = $now;
$this->updatedAt = $now;
}
/**
* Refreshes the updated timestamp prior to every update.
*/
#[ORM\PreUpdate]
public function onPreUpdate(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
/**
* Returns the database identifier.
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Gets the Spotify album identifier when sourced from Spotify.
*/
public function getSpotifyId(): ?string
{
return $this->spotifyId;
}
/**
* Stores the Spotify album identifier.
*/
public function setSpotifyId(?string $spotifyId): void
{
$this->spotifyId = $spotifyId;
}
/**
* Gets the local unique identifier for user-created albums.
*/
public function getLocalId(): ?string
{
return $this->localId;
}
/**
* Assigns the local identifier for user-created albums.
*/
public function setLocalId(?string $localId): void
{
$this->localId = $localId;
}
/**
* Returns the album source flag ("spotify" or "user").
*/
public function getSource(): string
{
return $this->source;
}
/**
* Sets the album source flag ("spotify" or "user").
*/
public function setSource(string $source): void
{
$this->source = $source;
}
/**
* Returns the human readable album title.
*/
public function getName(): string
{
return $this->name;
}
/**
* Updates the human readable album title.
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return list<string> Ordered performer names.
*/
public function getArtists(): array
{
return $this->artists;
}
/**
* @param list<string> $artists Ordered performer names.
*/
public function setArtists(array $artists): void
{
$this->artists = array_values($artists);
}
/**
* Returns the stored release date string.
*/
public function getReleaseDate(): ?string
{
return $this->releaseDate;
}
/**
* Sets the release date string (YYYY[-MM[-DD]]).
*/
public function setReleaseDate(?string $releaseDate): void
{
$this->releaseDate = $releaseDate;
}
/**
* Returns the total number of tracks.
*/
public function getTotalTracks(): int
{
return $this->totalTracks;
}
/**
* Sets the track count.
*/
public function setTotalTracks(int $totalTracks): void
{
$this->totalTracks = $totalTracks;
}
/**
* Returns the preferred cover art URL.
*/
public function getCoverUrl(): ?string
{
return $this->coverUrl;
}
/**
* Sets the preferred cover art URL.
*/
public function setCoverUrl(?string $coverUrl): void
{
$this->coverUrl = $coverUrl;
}
/**
* Returns an external link (defaults to Spotify).
*/
public function getExternalUrl(): ?string
{
return $this->externalUrl;
}
/**
* Sets the external reference link.
*/
public function setExternalUrl(?string $externalUrl): void
{
$this->externalUrl = $externalUrl;
}
/**
* Returns the user that created the album, when applicable.
*/
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
/**
* Returns the locally stored cover image path for user albums.
*/
public function getCoverImagePath(): ?string
{
return $this->coverImagePath;
}
/**
* Updates the locally stored cover image path for user albums.
*/
public function setCoverImagePath(?string $coverImagePath): void
{
$this->coverImagePath = $coverImagePath;
}
/**
* Sets the owner responsible for the album.
*/
public function setCreatedBy(?User $user): void
{
$this->createdBy = $user;
}
/**
* Returns the creation timestamp.
*/
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
/**
* Returns the last update timestamp.
*/
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
/**
* Shapes the entity to the payload Twig templates expect.
*
* @return array<string,mixed>
*/
public function toTemplateArray(): array
{
$images = [];
$imageUrl = $this->coverUrl;
if ($this->source === 'user' && $this->coverImagePath) {
$imageUrl = $this->coverImagePath;
}
if ($imageUrl) {
$images = [
['url' => $imageUrl],
['url' => $imageUrl],
];
}
$artists = array_map(static fn(string $n) => ['name' => $n], $this->artists);
$external = $this->externalUrl;
if ($external === null && $this->source === 'spotify' && $this->spotifyId) {
$external = 'https://open.spotify.com/album/' . $this->spotifyId;
}
$publicId = $this->source === 'user' ? (string) $this->localId : (string) $this->spotifyId;
return [
'id' => $publicId,
'name' => $this->name,
'images' => $images,
'artists' => $artists,
'release_date' => $this->releaseDate,
'total_tracks' => $this->totalTracks,
'external_urls' => [ 'spotify' => $external ],
'source' => $this->source,
];
}
}