Нет описания

SubscriberIPsRepository.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace MailPoet\Subscribers;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Doctrine\Repository;
  5. use MailPoet\Entities\SubscriberIPEntity;
  6. use MailPoetVendor\Carbon\Carbon;
  7. /**
  8. * @extends Repository<SubscriberIPEntity>
  9. */
  10. class SubscriberIPsRepository extends Repository {
  11. protected function getEntityClassName() {
  12. return SubscriberIPEntity::class;
  13. }
  14. public function findOneByIPAndCreatedAtAfterTimeInSeconds(string $ip, int $seconds): ?SubscriberIPEntity {
  15. return $this->entityManager->createQueryBuilder()
  16. ->select('sip')
  17. ->from(SubscriberIPEntity::class, 'sip')
  18. ->where('sip.ip = :ip')
  19. ->andWhere('sip.createdAt >= :timeThreshold')
  20. ->setParameter('ip', $ip)
  21. ->setParameter('timeThreshold', (new Carbon())->subSeconds($seconds))
  22. ->setMaxResults(1)
  23. ->getQuery()
  24. ->getOneOrNullResult();
  25. }
  26. public function getCountByIPAndCreatedAtAfterTimeInSeconds(string $ip, int $seconds): int {
  27. return $this->entityManager->createQueryBuilder()
  28. ->select('COUNT(sip)')
  29. ->from(SubscriberIPEntity::class, 'sip')
  30. ->where('sip.ip = :ip')
  31. ->andWhere('sip.createdAt >= :timeThreshold')
  32. ->setParameter('ip', $ip)
  33. ->setParameter('timeThreshold', (new Carbon())->subSeconds($seconds))
  34. ->getQuery()
  35. ->getSingleScalarResult();
  36. }
  37. public function deleteCreatedAtBeforeTimeInSeconds(int $seconds): int {
  38. return (int)$this->entityManager->createQueryBuilder()
  39. ->delete()
  40. ->from(SubscriberIPEntity::class, 'sip')
  41. ->where('sip.createdAt < :timeThreshold')
  42. ->setParameter('timeThreshold', (new Carbon())->subSeconds($seconds))
  43. ->getQuery()
  44. ->execute();
  45. }
  46. }