From 6d557c9a403025da50c465129fed7a46b462c40e Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sun, 12 Mar 2023 21:39:18 +0300 Subject: [PATCH] Ported Telegram\Account entity and repo. --- .../Repository/Telegram/AccountRepository.php | 41 ------ .../Entity/Telegram/Account.php | 132 ++++++------------ src/Repository/Telegram/AccountRepository.php | 51 +++++++ 3 files changed, 92 insertions(+), 132 deletions(-) delete mode 100644 old/src/PointToolsBundle/Repository/Telegram/AccountRepository.php rename {old/src/PointToolsBundle => src}/Entity/Telegram/Account.php (51%) create mode 100644 src/Repository/Telegram/AccountRepository.php diff --git a/old/src/PointToolsBundle/Repository/Telegram/AccountRepository.php b/old/src/PointToolsBundle/Repository/Telegram/AccountRepository.php deleted file mode 100644 index 34ba8c5..0000000 --- a/old/src/PointToolsBundle/Repository/Telegram/AccountRepository.php +++ /dev/null @@ -1,41 +0,0 @@ -getEntityManager()->persist($entity); - } - - /** - * Returns total number of accounts - * - * @return int - */ - public function getAccountsCount(): int - { - return $this->createQueryBuilder('a') - ->select('COUNT(a)') - ->getQuery()->getSingleScalarResult() - ; - } - - /** - * Returns number of accounts with linked Point.im profile - * - * @return int - */ - public function getLinkedAccountsCount(): int - { - return $this->createQueryBuilder('a') - ->select('COUNT(a)') - ->where('a.user IS NOT NULL') - ->getQuery()->getSingleScalarResult() - ; - } -} \ No newline at end of file diff --git a/old/src/PointToolsBundle/Entity/Telegram/Account.php b/src/Entity/Telegram/Account.php similarity index 51% rename from old/src/PointToolsBundle/Entity/Telegram/Account.php rename to src/Entity/Telegram/Account.php index edcc152..b4f7241 100644 --- a/old/src/PointToolsBundle/Entity/Telegram/Account.php +++ b/src/Entity/Telegram/Account.php @@ -1,106 +1,58 @@ 'subscriber_notification = TRUE', +])] +#[ORM\Index(columns: ['rename_notification'], name: 'rename_notification_idx', options: [ + 'where' => 'rename_notification = TRUE', +])] class Account { - /** - * Telegram user ID - * - * @var int - * - * @ORM\Id() - * @ORM\Column(name="account_id", type="integer") - */ - private $id; + #[ORM\Id] + #[ORM\Column(name: 'account_id', type: 'integer')] + private int $id; - /** - * @var \DateTime - * - * @ORM\Column(name="created_at", type="datetime") - */ - private $createdAt; + #[ORM\Column(name: '', type: 'datetime')] + private \DateTime $createdAt; - /** - * @var \DateTime - * - * @ORM\Column(name="updated_at", type="datetime", nullable=true) - */ - private $updatedAt; + #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: true)] + private ?\DateTime $updatedAt; - /** - * @var \DateTime - * - * @ORM\Column(name="linked_at", type="datetime", nullable=true) - */ - private $linkedAt; + #[ORM\Column(name: 'linked_at', type: 'datetime', nullable: true)] + private ?\DateTime $linkedAt; - /** - * @var string - * - * @ORM\Column(name="first_name", type="text") - */ - private $firstName; + #[ORM\Column(name: 'first_name', type: 'text')] + private string $firstName; - /** - * @var string|null - * - * @ORM\Column(name="last_name", type="text", nullable=true) - */ - private $lastName; + #[ORM\Column(name: 'last_name', type: 'text', nullable: true)] + private ?string $lastName; - /** - * @var string|null - * - * @ORM\Column(name="username", type="text", nullable=true) - */ - private $username; + #[ORM\Column(name: 'username', type: 'text', nullable: true)] + private ?string $username; - /** - * ID of private chat with user - * - * @var int - * - * @ORM\Column(name="private_chat_id", type="bigint", nullable=true) - */ - private $chatId; + #[ORM\Column(name: 'private_chat_id', type: 'bigint', nullable: true)] + private ?int $chatId; - /** - * @var User - * - * @ORM\OneToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\User") - * @ORM\JoinColumn(name="user_id", nullable=true, onDelete="CASCADE") - */ - private $user; + #[ORM\OneToOne(targetEntity: User::class)] + #[ORM\JoinColumn(name: 'user_id', nullable: true, onDelete: 'CASCADE')] + private ?User $user; - /** - * Notifications about new subscribers - * - * @var bool - * - * @ORM\Column(name="subscriber_notification", type="boolean") - */ - private $subscriberNotification = false; + #[ORM\Column(name: 'subscriber_notification', type: 'boolean')] + private bool $subscriberNotification = false; - /** - * Notifications about user renaming - * - * @var bool - * - * @ORM\Column(name="rename_notification", type="boolean") - */ - private $renameNotification = false; + #[ORM\Column(name: 'rename_notification', type: 'boolean')] + private bool $renameNotification = false; public function __construct(int $id) @@ -109,9 +61,7 @@ class Account $this->createdAt = new \DateTime(); } - /** - * @ORM\PreUpdate() - */ + #[ORM\PreUpdate] public function preUpdate(): void { $this->updatedAt = new \DateTime(); @@ -170,9 +120,9 @@ class Account return $this->user; } - public function setUser(User $user): Account + public function setUser(User $user): self { - if (!$this->user && $user) { + if ($this->user !== $user) { $this->linkedAt = new \DateTime(); } diff --git a/src/Repository/Telegram/AccountRepository.php b/src/Repository/Telegram/AccountRepository.php new file mode 100644 index 0000000..dcb89db --- /dev/null +++ b/src/Repository/Telegram/AccountRepository.php @@ -0,0 +1,51 @@ + + * + * @method Account|null find($id, $lockMode = null, $lockVersion = null) + * @method Account|null findOneBy(array $criteria, array $orderBy = null) + * @method Account[] findAll() + * @method Account[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class AccountRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Account::class); + } + + public function save(Account $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function getAccountsCount(): int + { + return $this->createQueryBuilder('a') + ->select('COUNT(a)') + ->getQuery()->getSingleScalarResult() + ; + } + + public function getLinkedAccountsCount(): int + { + return $this->createQueryBuilder('a') + ->select('COUNT(a)') + ->where('a.user IS NOT NULL') + ->getQuery()->getSingleScalarResult() + ; + } +}