Introducing AvatarSizeEnum.

This commit is contained in:
Alexey Skobkin 2023-08-17 02:17:24 +03:00
parent 392ead9b3e
commit 042d119cea
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
3 changed files with 19 additions and 17 deletions

View file

@ -5,8 +5,7 @@ declare(strict_types=1);
namespace App\Entity; namespace App\Entity;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\{ArrayCollection, Collection};
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UserRepository::class)] #[ORM\Entity(repositoryClass: UserRepository::class)]
@ -16,10 +15,6 @@ use Doctrine\ORM\Mapping as ORM;
#[ORM\Index(columns: ['is_removed'], name: 'idx_user_removed')] #[ORM\Index(columns: ['is_removed'], name: 'idx_user_removed')]
class User class User
{ {
public const AVATAR_SIZE_SMALL = '24';
public const AVATAR_SIZE_MEDIUM = '40';
public const AVATAR_SIZE_LARGE = '80';
#[ORM\Id] #[ORM\Id]
#[ORM\Column(name: 'id', type: 'integer')] #[ORM\Column(name: 'id', type: 'integer')]
private ?int $id = null; private ?int $id = null;

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Enum;
enum AvatarSizeEnum: int
{
case Small = 24;
case Medium = 40;
case Large = 80;
}

View file

@ -3,7 +3,7 @@ declare(strict_types=1);
namespace App\Twig; namespace App\Twig;
use App\Entity\User; use App\Enum\AvatarSizeEnum;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\{TwigFilter, TwigFunction}; use Twig\{TwigFilter, TwigFunction};
@ -43,20 +43,20 @@ class PointUrlExtension extends AbstractExtension
public function avatarSmallFunction(string $login): string public function avatarSmallFunction(string $login): string
{ {
return $this->avatarFunction($login, User::AVATAR_SIZE_SMALL); return $this->avatarFunction($login, AvatarSizeEnum::Small);
} }
public function avatarMediumFunction(string $login): string 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 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); return $this->getAvatarUrlByLogin($login, $size);
} }
@ -76,12 +76,8 @@ class PointUrlExtension extends AbstractExtension
return sprintf('%s://%s/%s', $this->pointScheme, $this->pointDomain, $postId); 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)) { return sprintf('%s://%s/avatar/%s/%s', $this->pointScheme, $this->pointDomain, \urlencode($login), $size->value);
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);
} }
} }