Files
piratepoet/mailpoet/lib/Util/DataInconsistency/DataInconsistencyController.php
Rostislav Wolny fe66e31b5f Add check for orphaned subscriptions
[MAILPOET-1587]
2024-08-08 15:37:32 +02:00

54 lines
2.1 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\Util\DataInconsistency;
use MailPoet\UnexpectedValueException;
class DataInconsistencyController {
const ORPHANED_SENDING_TASKS = 'orphaned_sending_tasks';
const ORPHANED_SENDING_TASK_SUBSCRIBERS = 'orphaned_sending_task_subscribers';
const SENDING_QUEUE_WITHOUT_NEWSLETTER = 'sending_queue_without_newsletter';
const ORPHANED_SUBSCRIPTIONS = 'orphaned_subscriptions';
const SUPPORTED_INCONSISTENCY_CHECKS = [
self::ORPHANED_SENDING_TASKS,
self::ORPHANED_SENDING_TASK_SUBSCRIBERS,
self::SENDING_QUEUE_WITHOUT_NEWSLETTER,
self::ORPHANED_SUBSCRIPTIONS,
];
private DataInconsistencyRepository $repository;
public function __construct(
DataInconsistencyRepository $repository
) {
$this->repository = $repository;
}
public function getInconsistentDataStatus(): array {
$result = [
self::ORPHANED_SENDING_TASKS => $this->repository->getOrphanedSendingTasksCount(),
self::ORPHANED_SENDING_TASK_SUBSCRIBERS => $this->repository->getOrphanedScheduledTasksSubscribersCount(),
self::SENDING_QUEUE_WITHOUT_NEWSLETTER => $this->repository->getSendingQueuesWithoutNewsletterCount(),
self::ORPHANED_SUBSCRIPTIONS => $this->repository->getOrphanedSubscriptionsCount(),
];
$result['total'] = array_sum($result);
return $result;
}
public function fixInconsistentData(string $inconsistency): void {
if (!in_array($inconsistency, self::SUPPORTED_INCONSISTENCY_CHECKS, true)) {
throw new UnexpectedValueException(__('Unsupported data inconsistency check.', 'mailpoet'));
}
if ($inconsistency === self::ORPHANED_SENDING_TASKS) {
$this->repository->cleanupOrphanedSendingTasks();
} elseif ($inconsistency === self::ORPHANED_SENDING_TASK_SUBSCRIBERS) {
$this->repository->cleanupOrphanedScheduledTaskSubscribers();
} elseif ($inconsistency === self::SENDING_QUEUE_WITHOUT_NEWSLETTER) {
$this->repository->cleanupSendingQueuesWithoutNewsletter();
} elseif ($inconsistency === self::ORPHANED_SUBSCRIPTIONS) {
$this->repository->cleanupOrphanedSubscriptions();
}
}
}