wtf
All checks were successful
CI - Build Tonehaus Docker image / tonehaus-ci-build (push) Successful in 2m0s
All checks were successful
CI - Build Tonehaus Docker image / tonehaus-ci-build (push) Successful in 2m0s
This commit is contained in:
144
src/Command/SeedDemoAlbumsCommand.php
Normal file
144
src/Command/SeedDemoAlbumsCommand.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Album;
|
||||
use App\Entity\User;
|
||||
use App\Repository\AlbumRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:seed-demo-albums',
|
||||
description: 'Create demo albums with randomized metadata for local development.'
|
||||
)]
|
||||
class SeedDemoAlbumsCommand extends Command
|
||||
{
|
||||
private const GENRES = [
|
||||
'Dreamwave', 'Synth Pop', 'Lo-Fi', 'Indie Rock', 'Chillhop', 'Neo Jazz',
|
||||
'Electro Funk', 'Ambient', 'Future Soul', 'Post Folk', 'Shoegaze', 'Hyperpop',
|
||||
];
|
||||
|
||||
private const ADJECTIVES = [
|
||||
'Electric', 'Velvet', 'Crimson', 'Solar', 'Golden', 'Neon', 'Silent', 'Liquid', 'Violet', 'Paper',
|
||||
];
|
||||
|
||||
private const NOUNS = [
|
||||
'Echoes', 'Horizons', 'Magnets', 'Parades', 'Cities', 'Signals', 'Fragments', 'Constellations',
|
||||
'Gardens', 'Drifters', 'Reflections', 'Blueprints',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly AlbumRepository $albumRepository,
|
||||
private readonly UserRepository $userRepository,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addOption('count', null, InputOption::VALUE_OPTIONAL, 'Number of demo albums to create', 40)
|
||||
->addOption('attach-users', null, InputOption::VALUE_NONE, 'If set, randomly assigns existing users as creators');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$count = max(1, (int) $input->getOption('count'));
|
||||
$attachUsers = (bool) $input->getOption('attach-users');
|
||||
$users = $attachUsers ? $this->userRepository->findAll() : [];
|
||||
|
||||
$created = 0;
|
||||
$seenLocalIds = [];
|
||||
|
||||
while ($created < $count) {
|
||||
$localId = $this->generateLocalId();
|
||||
if (isset($seenLocalIds[$localId]) || $this->albumRepository->findOneBy(['localId' => $localId]) !== null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$album = new Album();
|
||||
$album->setSource('user');
|
||||
$album->setLocalId($localId);
|
||||
$album->setName($this->generateAlbumName());
|
||||
$album->setArtists($this->generateArtists());
|
||||
$album->setReleaseDate($this->generateReleaseDate());
|
||||
$album->setTotalTracks(random_int(6, 16));
|
||||
$album->setCoverUrl($this->generateCoverUrl($localId));
|
||||
$album->setExternalUrl(sprintf('https://example.com/demo-albums/%s', $localId));
|
||||
|
||||
if ($attachUsers && $users !== []) {
|
||||
/** @var User $user */
|
||||
$user = $users[array_rand($users)];
|
||||
$album->setCreatedBy($user);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($album);
|
||||
$seenLocalIds[$localId] = true;
|
||||
$created++;
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$io->success(sprintf('Created %d demo albums%s.', $created, $attachUsers ? ' with random owners' : ''));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function generateLocalId(): string
|
||||
{
|
||||
return 'demo_' . bin2hex(random_bytes(4));
|
||||
}
|
||||
|
||||
private function generateAlbumName(): string
|
||||
{
|
||||
$adj = self::ADJECTIVES[random_int(0, count(self::ADJECTIVES) - 1)];
|
||||
$noun = self::NOUNS[random_int(0, count(self::NOUNS) - 1)];
|
||||
$genre = self::GENRES[random_int(0, count(self::GENRES) - 1)];
|
||||
|
||||
return sprintf('%s %s of %s', $adj, $noun, $genre);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function generateArtists(): array
|
||||
{
|
||||
$artists = [];
|
||||
$artistCount = random_int(1, 3);
|
||||
for ($i = 0; $i < $artistCount; $i++) {
|
||||
$artists[] = sprintf(
|
||||
'%s %s',
|
||||
self::ADJECTIVES[random_int(0, count(self::ADJECTIVES) - 1)],
|
||||
self::NOUNS[random_int(0, count(self::NOUNS) - 1)]
|
||||
);
|
||||
}
|
||||
|
||||
return array_values(array_unique($artists));
|
||||
}
|
||||
|
||||
private function generateReleaseDate(): string
|
||||
{
|
||||
$year = random_int(1990, (int) date('Y'));
|
||||
$month = random_int(1, 12);
|
||||
$day = random_int(1, 28);
|
||||
|
||||
return sprintf('%04d-%02d-%02d', $year, $month, $day);
|
||||
}
|
||||
|
||||
private function generateCoverUrl(string $seed): string
|
||||
{
|
||||
return sprintf('https://picsum.photos/seed/%s/640/640', $seed);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user