From 0988517bd0a8875ae41abca837835e1f34628b35 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Wed, 23 Jul 2025 23:19:19 -0500 Subject: [PATCH] fix: separates preference options from db to files --- .../Framework/Command/SeedDatabaseCommand.php | 73 +---------------- src/User/Database/CodecList.php | 25 ++++++ src/User/Database/ResolutionList.php | 27 ++++++ src/User/Dto/UserPreferencesFactory.php | 19 +---- .../Controller/Web/PreferencesController.php | 16 ++-- src/User/Framework/Entity/Preference.php | 41 ---------- .../Framework/Entity/PreferenceOption.php | 82 ------------------- src/User/Framework/Entity/User.php | 5 -- .../Form/GettingStartedFilterForm.php | 21 +---- .../Form/UserMediaPreferencesForm.php | 17 +--- .../Repository/PreferenceOptionRepository.php | 43 ---------- 11 files changed, 69 insertions(+), 300 deletions(-) create mode 100644 src/User/Database/CodecList.php create mode 100644 src/User/Database/ResolutionList.php delete mode 100644 src/User/Framework/Entity/PreferenceOption.php delete mode 100644 src/User/Framework/Repository/PreferenceOptionRepository.php diff --git a/src/Base/Framework/Command/SeedDatabaseCommand.php b/src/Base/Framework/Command/SeedDatabaseCommand.php index 5c25ede..7b30f49 100644 --- a/src/Base/Framework/Command/SeedDatabaseCommand.php +++ b/src/Base/Framework/Command/SeedDatabaseCommand.php @@ -20,17 +20,15 @@ use Symfony\Component\Console\Style\SymfonyStyle; class SeedDatabaseCommand extends Command { private PreferencesRepository $preferenceRepository; - private PreferenceOptionRepository $preferenceOptionRepository; + private UserRepository $userRepository; public function __construct( PreferencesRepository $preferenceRepository, - PreferenceOptionRepository $preferenceOptionRepository, UserRepository $userRepository, ) { parent::__construct(); $this->preferenceRepository = $preferenceRepository; - $this->preferenceOptionRepository = $preferenceOptionRepository; $this->userRepository = $userRepository; } @@ -39,7 +37,6 @@ class SeedDatabaseCommand extends Command $io = new SymfonyStyle($input, $output); $this->seedPreferences($io); - $this->seedPreferenceOptions($io); $this->updateUserPreferences($io); return Command::SUCCESS; @@ -140,72 +137,4 @@ class SeedDatabaseCommand extends Command ], ]; } - - private function seedPreferenceOptions(SymfonyStyle $io) - { - $io->info('[SeedDatabaseCommand] > Seeding preference options...'); - $options = $this->getPreferenceOptions(); - - foreach ($options as $option) { - if ($this->preferenceOptionRepository->findBy([ - 'preference' => $option['preference_id'], - 'name' => $option['name'], - 'value' => $option['value'], - 'enabled' => $option['enabled'], - ])) { - continue; - } - $this->preferenceOptionRepository->getEntityManager()->persist( - (new \App\User\Framework\Entity\PreferenceOption()) - ->setPreference($this->preferenceRepository->find($option['preference_id'])) - ->setName($option['name']) - ->setValue($option['value']) - ->setEnabled($option['enabled']) - ); - } - - $this->preferenceOptionRepository->getEntityManager()->flush(); - } - - private function getPreferenceOptions(): array - { - return [ - [ - 'preference_id' => 'resolution', - 'name' => '720p', - 'value' => '720p', - 'enabled' => true - ], - [ - 'preference_id' => 'resolution', - 'name' => '1080p', - 'value' => '1080p', - 'enabled' => true - ], - [ - 'preference_id' => 'resolution', - 'name' => '2160p', - 'value' => '2160p', - 'enabled' => true - ], - [ - 'preference_id' => 'codec', - 'name' => '-', - 'value' => '-', - 'enabled' => true - ], - [ - 'preference_id' => 'codec', - 'name' => 'h264', - 'value' => 'h264', - 'enabled' => true - ], - [ - 'preference_id' => 'codec', - 'name' => 'h265/HEVC', - 'value' => 'h265', - 'enabled' => true - ] - ]; - } } diff --git a/src/User/Database/CodecList.php b/src/User/Database/CodecList.php new file mode 100644 index 0000000..7068e9e --- /dev/null +++ b/src/User/Database/CodecList.php @@ -0,0 +1,25 @@ +getUserPreference($preferenceId); - if (null === $preference || "" === $preference || null === $preference->getPreferenceValue() || "" === $preference->getPreferenceValue()) { - return null; - } - return $preference->getPreference() - ->getPreferenceOptions() - ->filter(fn (PreferenceOption $option) => (string) $option->getId() === $preference->getPreferenceValue()) - ->first() - ->getId() - ; - } } diff --git a/src/User/Framework/Controller/Web/PreferencesController.php b/src/User/Framework/Controller/Web/PreferencesController.php index f2ef2bc..fd23550 100644 --- a/src/User/Framework/Controller/Web/PreferencesController.php +++ b/src/User/Framework/Controller/Web/PreferencesController.php @@ -79,11 +79,11 @@ class PreferencesController extends AbstractController SaveUserDownloadPreferencesHandler $saveUserDownloadPreferencesHandler, ): Response { - $downloadPreferences = $saveUserDownloadPreferencesHandler->handle($input->toCommand())->downloadPreferences; - $mediaPreferences = $this->getUser()->getMediaPreferences(); - - $languages = CountryLanguages::$languages; - sort($languages); + $downloadPreferences = $this->getUser()->getDownloadPreferences(); + $formData = (array) UserPreferencesFactory::createFromUser($this->getUser()); + $form = $this->createForm(UserMediaPreferencesForm::class, $formData); + + $saveUserDownloadPreferencesHandler->handle($input->toCommand()); $this->broadcaster->alert( title: 'Success', @@ -93,12 +93,8 @@ class PreferencesController extends AbstractController return $this->render( 'user/preferences.html.twig', [ - 'preferences' => $this->preferencesRepository->findEnabled(), - 'languages' => $languages, - 'providers' => ProviderList::getProviders(), - 'qualities' => QualityList::getBaseQualities(), - 'mediaPreferences' => $mediaPreferences, 'downloadPreferences' => $downloadPreferences, + 'preferences_form' => $form, ] ); } diff --git a/src/User/Framework/Entity/Preference.php b/src/User/Framework/Entity/Preference.php index 77c2be0..8a6637d 100644 --- a/src/User/Framework/Entity/Preference.php +++ b/src/User/Framework/Entity/Preference.php @@ -26,17 +26,6 @@ class Preference #[ORM\Column] private ?bool $enabled = null; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: PreferenceOption::class, mappedBy: 'preference', fetch: 'EAGER')] - private Collection $preferenceOptions; - - public function __construct() - { - $this->preferenceOptions = new ArrayCollection(); - } - public function getId(): ?string { return $this->id; @@ -94,34 +83,4 @@ class Preference return $this; } - - /** - * @return Collection - */ - public function getPreferenceOptions(): Collection - { - return $this->preferenceOptions; - } - - public function addPreferenceOption(PreferenceOption $preferenceOption): static - { - if (!$this->preferenceOptions->contains($preferenceOption)) { - $this->preferenceOptions->add($preferenceOption); - $preferenceOption->setPreference($this); - } - - return $this; - } - - public function removePreferenceOption(PreferenceOption $preferenceOption): static - { - if ($this->preferenceOptions->removeElement($preferenceOption)) { - // set the owning side to null (unless already changed) - if ($preferenceOption->getPreference() === $this) { - $preferenceOption->setPreference(null); - } - } - - return $this; - } } diff --git a/src/User/Framework/Entity/PreferenceOption.php b/src/User/Framework/Entity/PreferenceOption.php deleted file mode 100644 index 6fa7cad..0000000 --- a/src/User/Framework/Entity/PreferenceOption.php +++ /dev/null @@ -1,82 +0,0 @@ -id; - } - - public function getName(): ?string - { - return $this->name; - } - - public function setName(?string $name): static - { - $this->name = $name; - - return $this; - } - - public function getValue(): ?string - { - return $this->value; - } - - public function setValue(?string $value): static - { - $this->value = $value; - - return $this; - } - - public function getPreference(): ?Preference - { - return $this->preference; - } - - public function setPreference(?Preference $preference): static - { - $this->preference = $preference; - - return $this; - } - - public function isEnabled(): ?bool - { - return $this->enabled; - } - - public function setEnabled(bool $enabled): static - { - $this->enabled = $enabled; - - return $this; - } -} diff --git a/src/User/Framework/Entity/User.php b/src/User/Framework/Entity/User.php index 4c5cb0f..eb35837 100644 --- a/src/User/Framework/Entity/User.php +++ b/src/User/Framework/Entity/User.php @@ -215,11 +215,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface if (in_array($userPreference->getPreference()->getId(), ['language', 'provider', 'quality'])) { return $userPreference->getPreferenceValue(); } - foreach ($userPreference->getPreference()->getPreferenceOptions() as $preferenceOption) { - if ($preferenceOption->getId() === (int) $userPreference->getPreferenceValue()) { - return $preferenceOption->getValue(); - } - } return null; }) ->toArray(); diff --git a/src/User/Framework/Form/GettingStartedFilterForm.php b/src/User/Framework/Form/GettingStartedFilterForm.php index 3c01e77..751963c 100644 --- a/src/User/Framework/Form/GettingStartedFilterForm.php +++ b/src/User/Framework/Form/GettingStartedFilterForm.php @@ -2,11 +2,11 @@ namespace App\User\Framework\Form; -use Aimeos\Map; +use App\User\Database\CodecList; use App\User\Database\CountryLanguages; use App\User\Database\ProviderList; use App\User\Database\QualityList; -use App\User\Framework\Repository\PreferenceOptionRepository; +use App\User\Database\ResolutionList; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; @@ -14,17 +14,14 @@ use Symfony\Component\OptionsResolver\OptionsResolver; class GettingStartedFilterForm extends AbstractType { - public function __construct( - private readonly PreferenceOptionRepository $preferenceOptionRepository, - ) {} public function buildForm(FormBuilderInterface $builder, array $options): void { $this->addChoiceField($builder, 'language', CountryLanguages::asSelectOptions()); $this->addChoiceField($builder, 'quality', QualityList::asSelectOptions()); $this->addChoiceField($builder, 'provider', ProviderList::asSelectOptions()); - $this->addChoiceField($builder, 'resolution', $this->getPreferenceChoices('resolution')); - $this->addChoiceField($builder, 'codec', $this->getPreferenceChoices('codec')); + $this->addChoiceField($builder, 'resolution', ResolutionList::asSelectOptions()); + $this->addChoiceField($builder, 'codec', CodecList::asSelectOptions());; } private function addChoiceField(FormBuilderInterface $builder, string $fieldName, array $choices): void @@ -42,16 +39,6 @@ class GettingStartedFilterForm extends AbstractType $resolver->setDefaults([]); } - private function getPreferenceChoices(string $preference): array - { - $options = $this->preferenceOptionRepository->findBy(['preference' => $preference]); - $result = []; - foreach ($options as $item) { - $result[$item->getName()] = $item->getId(); - } - return $result; - } - private function addDefaultChoice(array $choices): iterable { return ['n/a' => null] + $choices; diff --git a/src/User/Framework/Form/UserMediaPreferencesForm.php b/src/User/Framework/Form/UserMediaPreferencesForm.php index c194a98..b7b00c4 100644 --- a/src/User/Framework/Form/UserMediaPreferencesForm.php +++ b/src/User/Framework/Form/UserMediaPreferencesForm.php @@ -3,9 +3,11 @@ namespace App\User\Framework\Form; use Aimeos\Map; +use App\User\Database\CodecList; use App\User\Database\CountryLanguages; use App\User\Database\ProviderList; use App\User\Database\QualityList; +use App\User\Database\ResolutionList; use App\User\Framework\Repository\PreferenceOptionRepository; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; @@ -17,7 +19,6 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class UserMediaPreferencesForm extends AbstractType { public function __construct( - private readonly PreferenceOptionRepository $preferenceOptionRepository, private readonly UrlGeneratorInterface $urlGenerator, ) {} @@ -26,8 +27,8 @@ class UserMediaPreferencesForm extends AbstractType $this->addChoiceField($builder, 'language', CountryLanguages::asSelectOptions()); $this->addChoiceField($builder, 'quality', QualityList::asSelectOptions()); $this->addChoiceField($builder, 'provider', ProviderList::asSelectOptions()); - $this->addChoiceField($builder, 'resolution', $this->getPreferenceChoices('resolution')); - $this->addChoiceField($builder, 'codec', $this->getPreferenceChoices('codec')); + $this->addChoiceField($builder, 'resolution', ResolutionList::asSelectOptions()); + $this->addChoiceField($builder, 'codec', CodecList::asSelectOptions()); } private function addChoiceField(FormBuilderInterface $builder, string $fieldName, array $choices): void @@ -48,16 +49,6 @@ class UserMediaPreferencesForm extends AbstractType ]); } - private function getPreferenceChoices(string $preference): array - { - $options = $this->preferenceOptionRepository->findBy(['preference' => $preference]); - $result = []; - foreach ($options as $item) { - $result[$item->getName()] = $item->getId(); - } - return $result; - } - private function addDefaultChoice(array $choices): iterable { return ['n/a' => ''] + $choices; diff --git a/src/User/Framework/Repository/PreferenceOptionRepository.php b/src/User/Framework/Repository/PreferenceOptionRepository.php deleted file mode 100644 index 3ed6ce5..0000000 --- a/src/User/Framework/Repository/PreferenceOptionRepository.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ -class PreferenceOptionRepository extends ServiceEntityRepository -{ - public function __construct(ManagerRegistry $registry) - { - parent::__construct($registry, PreferenceOption::class); - } - - // /** - // * @return PreferenceOption[] Returns an array of PreferenceOption objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('p') - // ->andWhere('p.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('p.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } - - // public function findOneBySomeField($value): ?PreferenceOption - // { - // return $this->createQueryBuilder('p') - // ->andWhere('p.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } -}