48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\User\Dto;
|
|
|
|
use App\User\Framework\Entity\PreferenceOption;
|
|
use App\User\Framework\Entity\User;
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
|
class UserPreferencesFactory
|
|
{
|
|
/** @param User $user */
|
|
public static function createFromUser(UserInterface $user): UserPreferences
|
|
{
|
|
return new UserPreferences(
|
|
resolution: self::getNestedValue($user, 'resolution'),
|
|
codec: self::getNestedValue($user, 'codec'),
|
|
language: self::getValue($user, 'language'),
|
|
provider: self::getValue($user, 'provider'),
|
|
quality: self::getValue($user, 'quality'),
|
|
);
|
|
}
|
|
|
|
/** @param User $user */
|
|
private static function getValue(UserInterface $user, string $preferenceId)
|
|
{
|
|
$value = $user->getUserPreference($preferenceId)->getPreferenceValue();
|
|
if ($value === "") {
|
|
return null;
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
/** @param User $user */
|
|
private static function getNestedValue(UserInterface $user, string $preferenceId): ?string
|
|
{
|
|
$preference = $user->getUserPreference($preferenceId);
|
|
if (null === $preference) {
|
|
return null;
|
|
}
|
|
return $preference->getPreference()
|
|
->getPreferenceOptions()
|
|
->filter(fn (PreferenceOption $option) => (string) $option->getId() === $preference->getPreferenceValue())
|
|
->first()
|
|
->getValue()
|
|
;
|
|
}
|
|
}
|