Replace all ORM::rawExecute() calls with Doctrine code
[MAILPOET-4377]
This commit is contained in:
@@ -6,24 +6,24 @@ use MailPoet\DI\ContainerWrapper;
|
|||||||
use MailPoet\Entities\NewsletterEntity;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Entities\NewsletterLinkEntity;
|
use MailPoet\Entities\NewsletterLinkEntity;
|
||||||
use MailPoet\Entities\NewsletterSegmentEntity;
|
use MailPoet\Entities\NewsletterSegmentEntity;
|
||||||
|
use MailPoet\Entities\ScheduledTaskEntity;
|
||||||
use MailPoet\Entities\SendingQueueEntity;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
use MailPoet\Entities\StatisticsClickEntity;
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
use MailPoet\Entities\StatisticsOpenEntity;
|
use MailPoet\Entities\StatisticsOpenEntity;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Models\ScheduledTask;
|
|
||||||
use MailPoet\Models\ScheduledTaskSubscriber;
|
use MailPoet\Models\ScheduledTaskSubscriber;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\StatisticsNewsletters;
|
use MailPoet\Models\StatisticsNewsletters;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
use MailPoet\Newsletter\Segment\NewsletterSegmentRepository;
|
use MailPoet\Newsletter\Segment\NewsletterSegmentRepository;
|
||||||
|
use MailPoet\Newsletter\Sending\ScheduledTasksRepository;
|
||||||
use MailPoet\Segments\SegmentsRepository;
|
use MailPoet\Segments\SegmentsRepository;
|
||||||
use MailPoet\Tasks\Sending;
|
use MailPoet\Tasks\Sending;
|
||||||
use MailPoet\Test\DataFactories\Newsletter;
|
use MailPoet\Test\DataFactories\Newsletter;
|
||||||
use MailPoet\Test\DataFactories\Segment;
|
use MailPoet\Test\DataFactories\Segment;
|
||||||
use MailPoetVendor\Carbon\Carbon;
|
use MailPoetVendor\Carbon\Carbon;
|
||||||
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
|
|
||||||
class WooCommercePastRevenues implements Generator {
|
class WooCommercePastRevenues implements Generator {
|
||||||
// Date range
|
// Date range
|
||||||
@@ -46,10 +46,14 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
/** @var NewsletterSegmentRepository */
|
/** @var NewsletterSegmentRepository */
|
||||||
private $newsletterSegmentRepository;
|
private $newsletterSegmentRepository;
|
||||||
|
|
||||||
|
/** @var ScheduledTasksRepository */
|
||||||
|
private $scheduledTasksRepository;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->entityManager = ContainerWrapper::getInstance()->get(EntityManager::class);
|
$this->entityManager = ContainerWrapper::getInstance()->get(EntityManager::class);
|
||||||
$this->segmentsRepository = ContainerWrapper::getInstance()->get(SegmentsRepository::class);
|
$this->segmentsRepository = ContainerWrapper::getInstance()->get(SegmentsRepository::class);
|
||||||
$this->newsletterSegmentRepository = ContainerWrapper::getInstance()->get(NewsletterSegmentRepository::class);
|
$this->newsletterSegmentRepository = ContainerWrapper::getInstance()->get(NewsletterSegmentRepository::class);
|
||||||
|
$this->scheduledTasksRepository = ContainerWrapper::getInstance()->get(ScheduledTasksRepository::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate() {
|
public function generate() {
|
||||||
@@ -278,8 +282,10 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function runBefore() {
|
public function runBefore() {
|
||||||
|
$connection = $this->entityManager->getConnection();
|
||||||
|
|
||||||
// Turn off CURRENT_TIMESTAMP to be able to save generated value
|
// Turn off CURRENT_TIMESTAMP to be able to save generated value
|
||||||
ORM::rawExecute(
|
$connection->executeStatement(
|
||||||
"ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "`
|
"ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "`
|
||||||
CHANGE `updated_at` `updated_at` timestamp NULL;"
|
CHANGE `updated_at` `updated_at` timestamp NULL;"
|
||||||
);
|
);
|
||||||
@@ -287,21 +293,23 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
// Disable keys
|
// Disable keys
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$prefix = $wpdb->prefix;
|
$prefix = $wpdb->prefix;
|
||||||
ORM::rawExecute("ALTER TABLE `{$prefix}posts` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `{$prefix}posts` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `{$prefix}postmeta` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `{$prefix}postmeta` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . Subscriber::$_table . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . Subscriber::$_table . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . SubscriberSegment::$_table . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . SubscriberSegment::$_table . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . $this->entityManager->getClassMetadata(NewsletterLinkEntity::class)->getTableName() . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(NewsletterLinkEntity::class)->getTableName() . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . ScheduledTask::$_table . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName() . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . ScheduledTaskSubscriber::$_table . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . ScheduledTaskSubscriber::$_table . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . SendingQueue::$_table . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . SendingQueue::$_table . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsOpenEntity::class)->getTableName() . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsOpenEntity::class)->getTableName() . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "` DISABLE KEYS");
|
||||||
ORM::rawExecute("SET UNIQUE_CHECKS = 0;");
|
$connection->executeStatement("SET UNIQUE_CHECKS = 0;");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function runAfter() {
|
public function runAfter() {
|
||||||
ORM::rawExecute(
|
$connection = $this->entityManager->getConnection();
|
||||||
|
|
||||||
|
$connection->executeStatement(
|
||||||
"ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "`
|
"ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "`
|
||||||
CHANGE `updated_at` `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;"
|
CHANGE `updated_at` `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;"
|
||||||
);
|
);
|
||||||
@@ -309,17 +317,17 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
// Enable keys
|
// Enable keys
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$prefix = $wpdb->prefix;
|
$prefix = $wpdb->prefix;
|
||||||
ORM::rawExecute("ALTER TABLE `{$prefix}posts` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `{$prefix}posts` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `{$prefix}postmeta` DISABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `{$prefix}postmeta` DISABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . Subscriber::$_table . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . Subscriber::$_table . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . SubscriberSegment::$_table . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . SubscriberSegment::$_table . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . $this->entityManager->getClassMetadata(NewsletterLinkEntity::class)->getTableName() . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(NewsletterLinkEntity::class)->getTableName() . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . ScheduledTask::$_table . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName() . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . ScheduledTaskSubscriber::$_table . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . ScheduledTaskSubscriber::$_table . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . SendingQueue::$_table . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . SendingQueue::$_table . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsOpenEntity::class)->getTableName() . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsOpenEntity::class)->getTableName() . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "` ENABLE KEYS");
|
$connection->executeStatement("ALTER TABLE `" . $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName() . "` ENABLE KEYS");
|
||||||
ORM::rawExecute("SET UNIQUE_CHECKS = 1;");
|
$connection->executeStatement("SET UNIQUE_CHECKS = 1;");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSubscriber($email, $lastName, $createdAtDate, \MailPoet\Models\Segment $segment, $status = Subscriber::STATUS_SUBSCRIBED) {
|
private function createSubscriber($email, $lastName, $createdAtDate, \MailPoet\Models\Segment $segment, $status = Subscriber::STATUS_SUBSCRIBED) {
|
||||||
@@ -353,19 +361,21 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function createSentEmailData(NewsletterEntity $newsletter, $sentAt, $subscribersIds, $segmentId) {
|
private function createSentEmailData(NewsletterEntity $newsletter, string $sentAt, $subscribersIds, $segmentId) {
|
||||||
|
$connection = $this->entityManager->getConnection();
|
||||||
|
|
||||||
// Sending task
|
// Sending task
|
||||||
$task = ScheduledTask::createOrUpdate([
|
$task = new ScheduledTaskEntity();
|
||||||
'type' => Sending::TASK_TYPE,
|
$task->setType(Sending::TASK_TYPE);
|
||||||
'status' => ScheduledTask::STATUS_COMPLETED,
|
$task->setStatus(ScheduledTaskEntity::STATUS_COMPLETED);
|
||||||
'created_at' => $sentAt,
|
$task->setCreatedAt(new Carbon($sentAt));
|
||||||
'processed_at' => $sentAt,
|
$task->setProcessedAt(new Carbon($sentAt));
|
||||||
]);
|
$this->scheduledTasksRepository->persist($task);
|
||||||
$task->save();
|
$this->scheduledTasksRepository->flush();
|
||||||
|
|
||||||
// Sending queue
|
// Sending queue
|
||||||
$queue = SendingQueue::createOrUpdate([
|
$queue = SendingQueue::createOrUpdate([
|
||||||
'task_id' => $task->id,
|
'task_id' => $task->getId(),
|
||||||
'newsletter_id' => $newsletter->getId(),
|
'newsletter_id' => $newsletter->getId(),
|
||||||
'count_total' => count($subscribersIds),
|
'count_total' => count($subscribersIds),
|
||||||
'count_processed' => count($subscribersIds),
|
'count_processed' => count($subscribersIds),
|
||||||
@@ -378,16 +388,16 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
$batchData = [];
|
$batchData = [];
|
||||||
$statsBatchData = [];
|
$statsBatchData = [];
|
||||||
foreach ($subscribersIds as $subscriberId) {
|
foreach ($subscribersIds as $subscriberId) {
|
||||||
$batchData[] = "({$task->id}, $subscriberId, 1, '$sentAt')";
|
$batchData[] = "({$task->getId()}, $subscriberId, 1, '$sentAt')";
|
||||||
if (count($batchData) % 1000 === 0) {
|
if (count($batchData) % 1000 === 0) {
|
||||||
ORM::rawExecute(
|
$connection->executeStatement(
|
||||||
"INSERT INTO " . ScheduledTaskSubscriber::$_table . " (`task_id`, `subscriber_id`, `processed`, `created_at`) VALUES " . implode(', ', $batchData)
|
"INSERT INTO " . ScheduledTaskSubscriber::$_table . " (`task_id`, `subscriber_id`, `processed`, `created_at`) VALUES " . implode(', ', $batchData)
|
||||||
);
|
);
|
||||||
$batchData = [];
|
$batchData = [];
|
||||||
}
|
}
|
||||||
$statsBatchData[] = "({$newsletter->getId()}, $subscriberId, $queue->id, '$sentAt')";
|
$statsBatchData[] = "({$newsletter->getId()}, $subscriberId, $queue->id, '$sentAt')";
|
||||||
if (count($statsBatchData) % 1000 === 0) {
|
if (count($statsBatchData) % 1000 === 0) {
|
||||||
ORM::rawExecute(
|
$connection->executeStatement(
|
||||||
"INSERT INTO " . StatisticsNewsletters::$_table . " (`newsletter_id`, `subscriber_id`, `queue_id`, `sent_at`) VALUES " . implode(', ', $statsBatchData)
|
"INSERT INTO " . StatisticsNewsletters::$_table . " (`newsletter_id`, `subscriber_id`, `queue_id`, `sent_at`) VALUES " . implode(', ', $statsBatchData)
|
||||||
);
|
);
|
||||||
$statsBatchData = [];
|
$statsBatchData = [];
|
||||||
@@ -395,12 +405,12 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($batchData) {
|
if ($batchData) {
|
||||||
ORM::rawExecute(
|
$connection->executeStatement(
|
||||||
"INSERT INTO " . ScheduledTaskSubscriber::$_table . " (`task_id`, `subscriber_id`, `processed`, `created_at`) VALUES " . implode(', ', $batchData)
|
"INSERT INTO " . ScheduledTaskSubscriber::$_table . " (`task_id`, `subscriber_id`, `processed`, `created_at`) VALUES " . implode(', ', $batchData)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if ($statsBatchData) {
|
if ($statsBatchData) {
|
||||||
ORM::rawExecute(
|
$connection->executeStatement(
|
||||||
"INSERT INTO " . StatisticsNewsletters::$_table . " (`newsletter_id`, `subscriber_id`, `queue_id`, `sent_at`) VALUES " . implode(', ', $statsBatchData)
|
"INSERT INTO " . StatisticsNewsletters::$_table . " (`newsletter_id`, `subscriber_id`, `queue_id`, `sent_at`) VALUES " . implode(', ', $statsBatchData)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -430,7 +440,7 @@ class WooCommercePastRevenues implements Generator {
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'newsletter_id' => $newsletter->getId(),
|
'newsletter_id' => $newsletter->getId(),
|
||||||
'task_id' => $task->id,
|
'task_id' => $task->getId(),
|
||||||
'queue_id' => $queue->id,
|
'queue_id' => $queue->id,
|
||||||
'sent_at' => strtotime($sentAt),
|
'sent_at' => strtotime($sentAt),
|
||||||
'link_id' => $link->getId(),
|
'link_id' => $link->getId(),
|
||||||
|
Reference in New Issue
Block a user