Remove usage old model from Throttling

[MAILPOET-3032]
This commit is contained in:
Jan Lysý
2021-04-02 17:11:07 +02:00
committed by Veljko V
parent 4631fb7320
commit 4d56020071
3 changed files with 77 additions and 40 deletions

View File

@ -4,6 +4,7 @@ namespace MailPoet\Subscribers;
use MailPoet\Doctrine\Repository;
use MailPoet\Entities\SubscriberIPEntity;
use MailPoetVendor\Carbon\Carbon;
/**
* @extends Repository<SubscriberIPEntity>
@ -12,4 +13,39 @@ class SubscriberIPsRepository extends Repository {
protected function getEntityClassName() {
return SubscriberIPEntity::class;
}
public function findOneByIPAndCreatedAtAfterTimeInSeconds(string $ip, int $seconds): ?SubscriberIPEntity {
return $this->entityManager->createQueryBuilder()
->select('sip')
->from(SubscriberIPEntity::class, 'sip')
->where('sip.ip = :ip')
->andWhere('sip.createdAt >= :timeThreshold')
->setParameter('ip', $ip)
->setParameter('timeThreshold', (new Carbon())->subSeconds($seconds))
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
public function getCountByIPAndCreatedAtAfterTimeInSeconds(string $ip, int $seconds): int {
return $this->entityManager->createQueryBuilder()
->select('COUNT(sip)')
->from(SubscriberIPEntity::class, 'sip')
->where('sip.ip = :ip')
->andWhere('sip.createdAt >= :timeThreshold')
->setParameter('ip', $ip)
->setParameter('timeThreshold', (new Carbon())->subSeconds($seconds))
->getQuery()
->getSingleScalarResult();
}
public function deleteCreatedAtBeforeTimeInSeconds(int $seconds): int {
return (int)$this->entityManager->createQueryBuilder()
->delete()
->from(SubscriberIPEntity::class, 'sip')
->where('sip.createdAt < :timeThreshold')
->setParameter('timeThreshold', (new Carbon())->subSeconds($seconds))
->getQuery()
->execute();
}
}