Compare commits

..

3 commits

Author SHA1 Message Date
Alexey Skobkin b413934d35
Introducing PostTypeEnum. 2023-08-17 02:27:31 +03:00
Alexey Skobkin 042d119cea
Introducing AvatarSizeEnum. 2023-08-17 02:17:24 +03:00
Alexey Skobkin 392ead9b3e
PointUrlExtension deprecation fixed. 2023-08-17 02:08:01 +03:00
7 changed files with 46 additions and 37 deletions

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\DataFixtures;
use App\Enum\Blog\PostTypeEnum;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
@ -28,31 +29,31 @@ class LoadPostData extends Fixture implements OrderedFixtureInterface
/** @var User $prWlUser */
$prWlUser = $this->getReference('test_user_'.LoadUserData::USER_PRWL_ID);
$longPost = (new Post(self::POST_ID_LONG, $mainUser, new \DateTime(), Post::TYPE_POST))
$longPost = (new Post(self::POST_ID_LONG, $mainUser, new \DateTime(), PostTypeEnum::Post))
->setText('Test post with many comments')
->setPrivate(false)
->setDeleted(false)
;
$shortPost = (new Post(self::POST_ID_SHORT, $mainUser, new \DateTime(), Post::TYPE_POST))
$shortPost = (new Post(self::POST_ID_SHORT, $mainUser, new \DateTime(), PostTypeEnum::Post))
->setText('Test short post')
->setPrivate(false)
->setDeleted(false)
;
$privateUserPost = (new Post(self::POST_ID_PR_USER, $privateUser, new \DateTime(), Post::TYPE_POST))
$privateUserPost = (new Post(self::POST_ID_PR_USER, $privateUser, new \DateTime(), PostTypeEnum::Post))
->setText('Post from private user. Should not be visible in the public feed.')
->setPrivate(false)
->setDeleted(false)
;
$wlUserPost = (new Post(self::POST_ID_WL_USER, $wlUser, new \DateTime(), Post::TYPE_POST))
$wlUserPost = (new Post(self::POST_ID_WL_USER, $wlUser, new \DateTime(), PostTypeEnum::Post))
->setText('Post from whitelist-only user. Should only be visible for whitelisted users.')
->setPrivate(false)
->setDeleted(false)
;
$privateWlUserPost = (new Post(self::POST_ID_PR_WL_USER, $prWlUser, new \DateTime(), Post::TYPE_POST))
$privateWlUserPost = (new Post(self::POST_ID_PR_WL_USER, $prWlUser, new \DateTime(), PostTypeEnum::Post))
->setText('Post from private AND whitelist-only user. Should not be visible in the public feed.')
->setPrivate(false)
->setDeleted(false)

View file

@ -4,9 +4,9 @@ declare(strict_types=1);
namespace App\Entity\Blog;
use App\Entity\User;
use App\Enum\Blog\PostTypeEnum;
use App\Repository\Blog\TagRepository;
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: TagRepository::class)]
@ -14,9 +14,6 @@ use Doctrine\ORM\Mapping as ORM;
#[ORM\Table(name: 'tags', schema: 'posts')]
class Post
{
public const TYPE_POST = 'post';
public const TYPE_FEED = 'feed';
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'text')]
private string $id;
@ -30,9 +27,8 @@ class Post
#[ORM\Column(name: 'updated_at', type: 'datetime', nullable: true)]
private ?\DateTime $updatedAt;
// TODO: Native Enum
#[ORM\Column(name: 'type', type: 'string', length: 6)]
private string $type = self::TYPE_POST;
#[ORM\Column(name: 'type', type: 'string', length: 6, enumType: PostTypeEnum::class)]
private PostTypeEnum $type;
#[ORM\Column(name: 'private', type: 'boolean', nullable: true)]
private bool $private;
@ -60,7 +56,7 @@ class Post
private Collection $comments;
public function __construct(string $id, User $author, \DateTime $createdAt, string $type)
public function __construct(string $id, User $author, \DateTime $createdAt, PostTypeEnum $type)
{
$this->id = $id;
$this->author = $author;
@ -105,7 +101,7 @@ class Post
return $this->updatedAt;
}
public function getType(): string
public function getType(): PostTypeEnum
{
return $this->type;
}

View file

@ -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;

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

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Enum\Blog;
enum PostTypeEnum: string
{
case Post = 'post';
case Feed = 'feed';
}

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Factory\Blog;
use App\Enum\Blog\PostTypeEnum;
use App\Factory\AbstractFactory;
use App\Factory\UserFactory;
use Doctrine\ORM\EntityManagerInterface;
@ -117,7 +118,7 @@ class PostFactory extends AbstractFactory
$postData->getId(),
$author,
new \DateTime($postData->getCreated()),
$postData->getType() ?: Post::TYPE_POST
PostTypeEnum::tryFrom($postData->getType()) ?? PostTypeEnum::Post,
);
$this->postRepository->save($post);
}

View file

@ -3,10 +3,9 @@ declare(strict_types=1);
namespace App\Twig;
use App\Entity\User;
use App\Enum\AvatarSizeEnum;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\{TwigFilter, TwigFunction};
class PointUrlExtension extends AbstractExtension
{
@ -16,7 +15,7 @@ class PointUrlExtension extends AbstractExtension
) {
}
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('point_avatar', [$this, 'avatarFunction']),
@ -29,7 +28,7 @@ class PointUrlExtension extends AbstractExtension
];
}
public function getFilters()
public function getFilters(): array
{
return [
new TwigFilter('point_avatar', [$this, 'avatarFunction']),
@ -44,20 +43,20 @@ class PointUrlExtension extends AbstractExtension
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
{
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);
}
@ -77,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);
}
}