Ported User entity and UserRepository.

This commit is contained in:
Alexey Skobkin 2023-03-12 18:52:54 +03:00
parent e482b4567a
commit 201356938f
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
3 changed files with 206 additions and 243 deletions

View file

@ -1,230 +0,0 @@
<?php
namespace src\PointToolsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use src\PointToolsBundle\Entity\Subscription;
use src\PointToolsBundle\Entity\SubscriptionEvent;
/**
* @ORM\Table(name="users", schema="users", indexes={
* @ORM\Index(name="idx_user_public", columns={"public"}),
* @ORM\Index(name="idx_user_removed", columns={"is_removed"})
* })
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks
*/
class User
{
public const AVATAR_SIZE_SMALL = '24';
public const AVATAR_SIZE_MEDIUM = '40';
public const AVATAR_SIZE_LARGE = '80';
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="login", type="string", length=255, nullable=false)
*/
private $login;
/**
* @var string|null
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @var bool
*
* @ORM\Column(name="public", type="boolean", nullable=false, options={"default": false})
*/
private $public = false;
/**
* @var bool
*
* @ORM\Column(name="whitelist_only", type="boolean", nullable=false, options={"default": false})
*/
private $whitelistOnly = false;
/**
* @var ArrayCollection|Subscription[]
*
* @ORM\OneToMany(targetEntity="Subscription", mappedBy="author", fetch="EXTRA_LAZY")
*/
private $subscribers;
/**
* @var ArrayCollection|Subscription[]
*
* @ORM\OneToMany(targetEntity="Subscription", mappedBy="subscriber", fetch="EXTRA_LAZY")
*/
private $subscriptions;
/**
* @var ArrayCollection|SubscriptionEvent[]
*
* @ORM\OneToMany(targetEntity="SubscriptionEvent", mappedBy="author", fetch="EXTRA_LAZY")
*/
private $newSubscriberEvents;
/**
* @var bool
*
* @ORM\Column(name="is_removed", type="boolean", options={"default": false})
*/
private $removed = false;
public function __construct(int $id, \DateTime $createdAt = null, string $login = null, string $name = null)
{
$this->id = $id;
$this->login = $login;
$this->name = $name;
$this->createdAt = $createdAt ?: new \DateTime();
$this->subscribers = new ArrayCollection();
$this->subscriptions = new ArrayCollection();
$this->newSubscriberEvents = new ArrayCollection();
}
/**
* @ORM\PreUpdate
*/
public function preUpdate(): void
{
$this->updatedAt = new \DateTime();
}
public function getId(): int
{
return $this->id;
}
public function getLogin(): string
{
return $this->login;
}
public function getName(): ?string
{
return $this->name;
}
public function updateLoginAndName(string $login, ?string $name): self
{
$this->login = $login;
$this->name = $name;
return $this;
}
public function addSubscriber(Subscription $subscribers): self
{
$this->subscribers[] = $subscribers;
return $this;
}
public function removeSubscriber(Subscription $subscribers)
{
$this->subscribers->removeElement($subscribers);
}
/**
* @return Subscription[]|ArrayCollection
*/
public function getSubscribers(): iterable
{
return $this->subscribers;
}
/**
* @return Subscription[]|ArrayCollection
*/
public function getSubscriptions(): iterable
{
return $this->subscriptions;
}
public function addNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents): self
{
$this->newSubscriberEvents[] = $newSubscriberEvents;
return $this;
}
/**
* @return SubscriptionEvent[]|ArrayCollection
*/
public function getNewSubscriberEvents(): iterable
{
return $this->newSubscriberEvents;
}
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
public function updatePrivacy(?bool $public, ?bool $whitelistOnly): void
{
$this->public = $public;
$this->whitelistOnly = $whitelistOnly;
}
public function isPublic(): ?bool
{
return $this->public;
}
public function isWhitelistOnly(): ?bool
{
return $this->whitelistOnly;
}
public function isRemoved(): bool
{
return $this->removed;
}
public function markAsRemoved(): void
{
$this->removed = true;
}
public function restore(): void
{
$this->removed = false;
}
}

178
src/Entity/User.php Normal file
View file

@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'users', schema: 'users')]
#[ORM\Index(columns: ['public'], name: 'idx_user_public')]
#[ORM\Index(columns: ['is_removed'], name: 'idx_user_removed')]
#[ORM\HasLifecycleCallbacks]
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;
#[ORM\Column(name: 'login', type: 'string', length: 255, nullable: false)]
private string $login;
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
private ?string $name;
#[ORM\Column(name: 'created_at', type: 'datetime')]
private \DateTime $createdAt;
#[ORM\Column(name: 'updated_at', type: 'datetime', nullable: true)]
private \DateTime $updatedAt;
#[ORM\Column(name: 'public', type: 'boolean', nullable: false, options: ['default' => false])]
private bool $public = false;
#[ORM\Column(name: 'whitelist_only', type: 'boolean', nullable: false, options: ['default' => false])]
private bool $whitelistOnly = false;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Subscription::class, fetch: 'EXTRA_LAZY')]
private ArrayCollection $subscribers;
#[ORM\OneToMany(mappedBy: 'subscriber', targetEntity: Subscription::class, fetch: 'EXTRA_LAZY')]
private ArrayCollection $subscriptions;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: SubscriptionEvent::class, fetch: 'EXTRA_LAZY')]
private ArrayCollection $newSubscriberEvents;
#[ORM\Column(name: 'is_removed', type: 'boolean', options: ['default' => false])]
private bool $removed = false;
public function __construct(
int $id,
\DateTime $createdAt = null,
string $login = null,
string $name = null
) {
$this->id = $id;
$this->login = $login;
$this->name = $name;
$this->createdAt = $createdAt ?: new \DateTime();
$this->subscribers = new ArrayCollection();
$this->subscriptions = new ArrayCollection();
$this->newSubscriberEvents = new ArrayCollection();
}
#[ORM\PreUpdate]
public function preUpdate(): void
{
$this->updatedAt = new \DateTime();
}
public function getId(): int
{
return $this->id;
}
public function getLogin(): string
{
return $this->login;
}
public function getName(): ?string
{
return $this->name;
}
public function updateLoginAndName(string $login, ?string $name): self
{
$this->login = $login;
$this->name = $name;
return $this;
}
public function addSubscriber(Subscription $subscribers): self
{
$this->subscribers[] = $subscribers;
return $this;
}
public function removeSubscriber(Subscription $subscribers)
{
$this->subscribers->removeElement($subscribers);
}
/** @return Subscription[]|ArrayCollection */
public function getSubscribers(): ArrayCollection
{
return $this->subscribers;
}
/** @return Subscription[]|ArrayCollection */
public function getSubscriptions(): ArrayCollection
{
return $this->subscriptions;
}
public function addNewSubscriberEvent(SubscriptionEvent $newSubscriberEvents): self
{
$this->newSubscriberEvents[] = $newSubscriberEvents;
return $this;
}
/** @return SubscriptionEvent[]|ArrayCollection */
public function getNewSubscriberEvents(): ArrayCollection
{
return $this->newSubscriberEvents;
}
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
public function updatePrivacy(?bool $public, ?bool $whitelistOnly): void
{
$this->public = $public;
$this->whitelistOnly = $whitelistOnly;
}
public function isPublic(): ?bool
{
return $this->public;
}
public function isWhitelistOnly(): bool
{
return $this->whitelistOnly;
}
public function isRemoved(): bool
{
return $this->removed;
}
public function markAsRemoved(): void
{
$this->removed = true;
}
public function restore(): void
{
$this->removed = false;
}
}

View file

@ -1,16 +1,35 @@
<?php <?php
namespace src\PointToolsBundle\Repository; declare(strict_types=1);
use Doctrine\ORM\EntityRepository; namespace App\Repository;
use src\PointToolsBundle\DTO\TopUserDTO;
use src\PointToolsBundle\Entity\User;
class UserRepository extends EntityRepository use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository
{ {
public function add(User $entity) public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function save(User $entity, bool $flush = false): void
{ {
$this->getEntityManager()->persist($entity); $this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
} }
public function findActiveUserWithSubscribers(int $id): ?User public function findActiveUserWithSubscribers(int $id): ?User
@ -29,9 +48,7 @@ class UserRepository extends EntityRepository
; ;
} }
/** /** Case-insensitive user search */
* Case-insensitive user search
*/
public function findUserByLogin(string $login): ?User public function findUserByLogin(string $login): ?User
{ {
$qb = $this->createQueryBuilder('u'); $qb = $this->createQueryBuilder('u');
@ -75,9 +92,7 @@ class UserRepository extends EntityRepository
return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult(); return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult();
} }
/** /** @return User[] */
* @return User[]
*/
public function findUserSubscribersById(int $id): array public function findUserSubscribersById(int $id): array
{ {
$qb = $this->createQueryBuilder('u'); $qb = $this->createQueryBuilder('u');