Files
piratepoet/lib/NewsletterTemplates/NewsletterTemplatesRepository.php
Rostislav Wolny 5134713c2d Optimize memory usage for batch processing of template images
NewsletterTemplateEntity carries base64 data of image. This commit changes how we load templates entities when we process thumbnail images so that there is only one template in memory.
[MAILPOET-2686]
2021-11-08 13:24:53 +01:00

112 lines
3.4 KiB
PHP

<?php
namespace MailPoet\NewsletterTemplates;
use MailPoet\Doctrine\Repository;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\NewsletterTemplateEntity;
/**
* @extends Repository<NewsletterTemplateEntity>
*/
class NewsletterTemplatesRepository extends Repository {
const RECENTLY_SENT_CATEGORIES = '["recent"]';
const RECENTLY_SENT_COUNT = 12;
protected function getEntityClassName() {
return NewsletterTemplateEntity::class;
}
/**
* @return NewsletterTemplateEntity[]
*/
public function findAllForListing(): array {
return $this->doctrineRepository->createQueryBuilder('nt')
->select('PARTIAL nt.{id,categories,thumbnail,name,readonly}')
->addOrderBy('nt.readonly', 'ASC')
->addOrderBy('nt.createdAt', 'DESC')
->addOrderBy('nt.id', 'DESC')
->getQuery()
->getResult();
}
public function createOrUpdate(array $data): NewsletterTemplateEntity {
$template = !empty($data['newsletter_id'])
? $this->findOneBy(['newsletter' => (int)$data['newsletter_id']])
: null;
if (!$template) {
$template = new NewsletterTemplateEntity($data['name'] ?? '');
$this->entityManager->persist($template);
}
if (isset($data['newsletter_id'])) {
$template->setNewsletter($this->entityManager->getReference(NewsletterEntity::class, (int)$data['newsletter_id']));
}
if (isset($data['name'])) {
$template->setName($data['name']);
}
if (isset($data['thumbnail'])) {
$template->setThumbnail($data['thumbnail']);
}
if (isset($data['thumbnail_data'])) {
$template->setThumbnailData($data['thumbnail_data']);
}
if (isset($data['body'])) {
$template->setBody(json_decode($data['body'], true));
}
if (isset($data['categories'])) {
$template->setCategories($data['categories']);
}
$this->entityManager->flush();
return $template;
}
public function cleanRecentlySent() {
// fetch 'RECENTLY_SENT_COUNT' of most recent template IDs in 'RECENTLY_SENT_CATEGORIES'
$recentIds = $this->doctrineRepository->createQueryBuilder('nt')
->select('nt.id')
->where('nt.categories = :categories')
->setParameter('categories', self::RECENTLY_SENT_CATEGORIES)
->orderBy('nt.id', 'DESC')
->setMaxResults(self::RECENTLY_SENT_COUNT)
->getQuery()
->getResult();
// delete all 'RECENTLY_SENT_CATEGORIES' templates except the latest ones selected above
$this->entityManager->createQueryBuilder()
->delete(NewsletterTemplateEntity::class, 'nt')
->where('nt.categories = :categories')
->andWhere('nt.id NOT IN (:recentIds)')
->setParameter('categories', self::RECENTLY_SENT_CATEGORIES)
->setParameter('recentIds', array_column($recentIds, 'id'))
->getQuery()
->execute();
}
public function getRecentlySentCount(): int {
return (int)$this->doctrineRepository->createQueryBuilder('nt')
->select('COUNT(nt.id)')
->where('nt.categories = :categories')
->setParameter('categories', self::RECENTLY_SENT_CATEGORIES)
->getQuery()
->getSingleScalarResult();
}
public function getIdsOfEditableTemplates(): array {
$result = $this->doctrineRepository->createQueryBuilder('nt')
->select('nt.id')
->where('nt.readonly = :readonly')
->setParameter('readonly', false)
->getQuery()
->getArrayResult();
return array_column($result, 'id');
}
}