From 042d119cea53106cf0ad26269693b013fb68b765 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 17 Aug 2023 02:17:24 +0300 Subject: [PATCH] Introducing AvatarSizeEnum. --- src/Entity/User.php | 7 +------ src/Enum/AvatarSizeEnum.php | 11 +++++++++++ src/Twig/PointUrlExtension.php | 18 +++++++----------- 3 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 src/Enum/AvatarSizeEnum.php diff --git a/src/Entity/User.php b/src/Entity/User.php index bb7c1f2..2d72bee 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -5,8 +5,7 @@ declare(strict_types=1); namespace App\Entity; use App\Repository\UserRepository; -use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\{ArrayCollection, Collection}; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: UserRepository::class)] @@ -16,10 +15,6 @@ use Doctrine\ORM\Mapping as ORM; #[ORM\Index(columns: ['is_removed'], name: 'idx_user_removed')] class User { - public const AVATAR_SIZE_SMALL = '24'; - public const AVATAR_SIZE_MEDIUM = '40'; - public const AVATAR_SIZE_LARGE = '80'; - #[ORM\Id] #[ORM\Column(name: 'id', type: 'integer')] private ?int $id = null; diff --git a/src/Enum/AvatarSizeEnum.php b/src/Enum/AvatarSizeEnum.php new file mode 100644 index 0000000..1d83436 --- /dev/null +++ b/src/Enum/AvatarSizeEnum.php @@ -0,0 +1,11 @@ +avatarFunction($login, User::AVATAR_SIZE_SMALL); + return $this->avatarFunction($login, AvatarSizeEnum::Small); } public function avatarMediumFunction(string $login): string { - return $this->avatarFunction($login, User::AVATAR_SIZE_MEDIUM); + return $this->avatarFunction($login, AvatarSizeEnum::Medium); } public function avatarLargeFunction(string $login): string { - return $this->avatarFunction($login, User::AVATAR_SIZE_LARGE); + return $this->avatarFunction($login, AvatarSizeEnum::Large); } - public function avatarFunction(string $login, $size): string + public function avatarFunction(string $login, AvatarSizeEnum $size): string { return $this->getAvatarUrlByLogin($login, $size); } @@ -76,12 +76,8 @@ class PointUrlExtension extends AbstractExtension return sprintf('%s://%s/%s', $this->pointScheme, $this->pointDomain, $postId); } - private function getAvatarUrlByLogin(string $login, string $size): string + private function getAvatarUrlByLogin(string $login, AvatarSizeEnum $size): string { - if (!in_array($size, [User::AVATAR_SIZE_SMALL, User::AVATAR_SIZE_MEDIUM, User::AVATAR_SIZE_LARGE], true)) { - throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See User::AVATAR_SIZE_* constants.'); - } - - return sprintf('%s://%s/avatar/%s/%s', $this->pointScheme, $this->pointDomain, urlencode($login), $size); + return sprintf('%s://%s/avatar/%s/%s', $this->pointScheme, $this->pointDomain, \urlencode($login), $size->value); } }