Ported SubscriptionEvent entity and SubscriptionEventRepository.

This commit is contained in:
Alexey Skobkin 2023-03-12 20:50:52 +03:00
parent 5b0fd11f31
commit 4f37cb9782
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
3 changed files with 103 additions and 124 deletions

View file

@ -1,93 +0,0 @@
<?php
namespace src\PointToolsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use src\PointToolsBundle\Entity\User;
/**
* @ORM\Table(name="log", schema="subscriptions", indexes={
* @ORM\Index(name="idx_subscription_author", columns={"author_id"}),
* @ORM\Index(name="idx_subscription_subscriber", columns={"subscriber_id"}),
* @ORM\Index(name="idx_subscription_date", columns={"date"})
* })
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository", readOnly=true)
*/
class SubscriptionEvent
{
public const ACTION_SUBSCRIBE = 'subscribe';
public const ACTION_UNSUBSCRIBE = 'unsubscribe';
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var User Blog author
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="newSubscriberEvents")
* @ORM\JoinColumn(name="author_id", nullable=false)
*/
private $author;
/**
* @var User Blog subscriber
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="subscriber_id", nullable=false)
*/
private $subscriber;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* @var string
*
* @ORM\Column(name="action", type="string", length=12, nullable=false)
*/
private $action;
public function __construct(User $author, User $subscriber, string $action = self::ACTION_SUBSCRIBE)
{
$this->author = $author;
$this->subscriber = $subscriber;
$this->action = $action;
$this->date = new \DateTime();
}
public function getId(): int
{
return $this->id;
}
public function getDate(): \DateTime
{
return $this->date;
}
public function getSubscriber(): User
{
return $this->subscriber;
}
public function getAuthor(): User
{
return $this->author;
}
public function getAction(): string
{
return $this->action;
}
}

View file

@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\SubscriptionEventRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SubscriptionEventRepository::class, readOnly: true)]
#[ORM\Table(name: 'log', schema: 'subscriptions')]
#[ORM\Index(columns: ['author_id'], name: 'idx_subscription_author')]
#[ORM\Index(columns: ['subscriber_id'], name: 'idx_subscription_subscriber')]
#[ORM\Index(columns: ['date'], name: 'idx_subscription_date')]
class SubscriptionEvent
{
public const ACTION_SUBSCRIBE = 'subscribe';
public const ACTION_UNSUBSCRIBE = 'unsubscribe';
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(name: 'id', type: 'integer')]
private ?int $id;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'newSubscriberEvents')]
#[ORM\JoinColumn(name: 'author_id', nullable: false)]
private User $author;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'subscriber_id', nullable: false)]
private User $subscriber;
#[ORM\Column(name: 'date', type: 'datetime', nullable: false)]
private \DateTime $date;
#[ORM\Column(name: 'action', type: 'string', length: 12, nullable: false)]
private string $action;
public function __construct(User $author, User $subscriber, string $action = self::ACTION_SUBSCRIBE)
{
$this->author = $author;
$this->subscriber = $subscriber;
$this->action = $action;
$this->date = new \DateTime();
}
public function getId(): int
{
return $this->id;
}
public function getDate(): \DateTime
{
return $this->date;
}
public function getSubscriber(): User
{
return $this->subscriber;
}
public function getAuthor(): User
{
return $this->author;
}
public function getAction(): string
{
return $this->action;
}
}

View file

@ -1,22 +1,39 @@
<?php <?php
namespace src\PointToolsBundle\Repository; declare(strict_types=1);
use Doctrine\ORM\EntityRepository; namespace App\Repository;
use App\Entity\SubscriptionEvent;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use src\PointToolsBundle\Entity\SubscriptionEvent; use Doctrine\Persistence\ManagerRegistry;
use src\PointToolsBundle\Entity\User;
class SubscriptionEventRepository extends EntityRepository /**
* @extends ServiceEntityRepository<SubscriptionEvent>
*
* @method SubscriptionEvent|null find($id, $lockMode = null, $lockVersion = null)
* @method SubscriptionEvent|null findOneBy(array $criteria, array $orderBy = null)
* @method SubscriptionEvent[] findAll()
* @method SubscriptionEvent[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class SubscriptionEventRepository extends ServiceEntityRepository
{ {
public function add(SubscriptionEvent $entity): void public function __construct(ManagerRegistry $registry)
{ {
$this->getEntityManager()->persist($entity); parent::__construct($registry, SubscriptionEvent::class);
}
public function save(SubscriptionEvent $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
} }
/**
* @return int
*/
public function getLastDayEventsCount(): int public function getLastDayEventsCount(): int
{ {
$qb = $this->createQueryBuilder('se'); $qb = $this->createQueryBuilder('se');
@ -31,13 +48,7 @@ class SubscriptionEventRepository extends EntityRepository
; ;
} }
/** /** Creates QueryBuilder object for pagination of user subscribers events */
* Creates QueryBuilder object for pagination of user subscribers events
*
* @param User $user
*
* @return QueryBuilder
*/
public function createUserLastSubscribersEventsQuery(User $user): QueryBuilder public function createUserLastSubscribersEventsQuery(User $user): QueryBuilder
{ {
$qb = $this->createQueryBuilder('se'); $qb = $this->createQueryBuilder('se');
@ -54,9 +65,6 @@ class SubscriptionEventRepository extends EntityRepository
/** /**
* Get last user subscriber events * Get last user subscriber events
* *
* @param User $user
* @param int $limit
*
* @return SubscriptionEvent[] * @return SubscriptionEvent[]
*/ */
public function getUserLastSubscribersEvents(User $user, int $limit = 20): array public function getUserLastSubscribersEvents(User $user, int $limit = 20): array
@ -67,11 +75,7 @@ class SubscriptionEventRepository extends EntityRepository
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();
} }
/** /** Get last global subscriptions QueryBuilder for pagination */
* Get last global subscriptions QueryBuilder for pagination
*
* @return QueryBuilder
*/
public function createLastSubscriptionEventsQuery(): QueryBuilder public function createLastSubscriptionEventsQuery(): QueryBuilder
{ {
$qb = $this->createQueryBuilder('se'); $qb = $this->createQueryBuilder('se');
@ -87,8 +91,6 @@ class SubscriptionEventRepository extends EntityRepository
/** /**
* Get last global subscription events * Get last global subscription events
* *
* @param int $limit
*
* @return SubscriptionEvent[] * @return SubscriptionEvent[]
*/ */
public function getLastSubscriptionEvents(int $limit = 20): array public function getLastSubscriptionEvents(int $limit = 20): array
@ -99,9 +101,7 @@ class SubscriptionEventRepository extends EntityRepository
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();
} }
/** /** @return SubscriptionEvent[] */
* @return SubscriptionEvent[]
*/
public function getLastEventsByDay(int $days = 30): array public function getLastEventsByDay(int $days = 30): array
{ {
$qb = $this->createQueryBuilder('se'); $qb = $this->createQueryBuilder('se');