DataInconsistencyController.php
2 weeks ago
DataInconsistencyRepository.php
2 weeks ago
index.php
1 year ago
DataInconsistencyController.php
77 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Util\DataInconsistency; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\UnexpectedValueException; |
| 9 | |
| 10 | class DataInconsistencyController { |
| 11 | const ORPHANED_SENDING_TASKS = 'orphaned_sending_tasks'; |
| 12 | const ORPHANED_SENDING_TASK_SUBSCRIBERS = 'orphaned_sending_task_subscribers'; |
| 13 | const SENDING_QUEUE_WITHOUT_NEWSLETTER = 'sending_queue_without_newsletter'; |
| 14 | const ORPHANED_SUBSCRIPTIONS = 'orphaned_subscriptions'; |
| 15 | const ORPHANED_SUBSCRIBER_CUSTOM_FIELDS = 'orphaned_subscriber_custom_fields'; |
| 16 | const ORPHANED_SUBSCRIBER_TAGS = 'orphaned_subscriber_tags'; |
| 17 | const ORPHANED_LINKS = 'orphaned_links'; |
| 18 | const ORPHANED_NEWSLETTER_POSTS = 'orphaned_newsletter_posts'; |
| 19 | |
| 20 | const SUPPORTED_INCONSISTENCY_CHECKS = [ |
| 21 | self::ORPHANED_SENDING_TASKS, |
| 22 | self::ORPHANED_SENDING_TASK_SUBSCRIBERS, |
| 23 | self::SENDING_QUEUE_WITHOUT_NEWSLETTER, |
| 24 | self::ORPHANED_SUBSCRIPTIONS, |
| 25 | self::ORPHANED_SUBSCRIBER_CUSTOM_FIELDS, |
| 26 | self::ORPHANED_SUBSCRIBER_TAGS, |
| 27 | self::ORPHANED_LINKS, |
| 28 | self::ORPHANED_NEWSLETTER_POSTS, |
| 29 | ]; |
| 30 | |
| 31 | private DataInconsistencyRepository $repository; |
| 32 | |
| 33 | public function __construct( |
| 34 | DataInconsistencyRepository $repository |
| 35 | ) { |
| 36 | $this->repository = $repository; |
| 37 | } |
| 38 | |
| 39 | public function getInconsistentDataStatus(): array { |
| 40 | $result = [ |
| 41 | self::ORPHANED_SENDING_TASKS => $this->repository->getOrphanedSendingTasksCount(), |
| 42 | self::ORPHANED_SENDING_TASK_SUBSCRIBERS => $this->repository->getOrphanedScheduledTasksSubscribersCount(), |
| 43 | self::SENDING_QUEUE_WITHOUT_NEWSLETTER => $this->repository->getSendingQueuesWithoutNewsletterCount(), |
| 44 | self::ORPHANED_SUBSCRIPTIONS => $this->repository->getOrphanedSubscriptionsCount(), |
| 45 | self::ORPHANED_SUBSCRIBER_CUSTOM_FIELDS => $this->repository->getOrphanedSubscriberCustomFieldsCount(), |
| 46 | self::ORPHANED_SUBSCRIBER_TAGS => $this->repository->getOrphanedSubscriberTagsCount(), |
| 47 | self::ORPHANED_LINKS => $this->repository->getOrphanedNewsletterLinksCount(), |
| 48 | self::ORPHANED_NEWSLETTER_POSTS => $this->repository->getOrphanedNewsletterPostsCount(), |
| 49 | ]; |
| 50 | $result['total'] = array_sum($result); |
| 51 | return $result; |
| 52 | } |
| 53 | |
| 54 | public function fixInconsistentData(string $inconsistency): void { |
| 55 | if (!in_array($inconsistency, self::SUPPORTED_INCONSISTENCY_CHECKS, true)) { |
| 56 | throw new UnexpectedValueException(__('Unsupported data inconsistency check.', 'mailpoet')); |
| 57 | } |
| 58 | if ($inconsistency === self::ORPHANED_SENDING_TASKS) { |
| 59 | $this->repository->cleanupOrphanedSendingTasks(); |
| 60 | } elseif ($inconsistency === self::ORPHANED_SENDING_TASK_SUBSCRIBERS) { |
| 61 | $this->repository->cleanupOrphanedScheduledTaskSubscribers(); |
| 62 | } elseif ($inconsistency === self::SENDING_QUEUE_WITHOUT_NEWSLETTER) { |
| 63 | $this->repository->cleanupSendingQueuesWithoutNewsletter(); |
| 64 | } elseif ($inconsistency === self::ORPHANED_SUBSCRIPTIONS) { |
| 65 | $this->repository->cleanupOrphanedSubscriptions(); |
| 66 | } elseif ($inconsistency === self::ORPHANED_SUBSCRIBER_CUSTOM_FIELDS) { |
| 67 | $this->repository->cleanupOrphanedSubscriberCustomFields(); |
| 68 | } elseif ($inconsistency === self::ORPHANED_SUBSCRIBER_TAGS) { |
| 69 | $this->repository->cleanupOrphanedSubscriberTags(); |
| 70 | } elseif ($inconsistency === self::ORPHANED_LINKS) { |
| 71 | $this->repository->cleanupOrphanedNewsletterLinks(); |
| 72 | } elseif ($inconsistency === self::ORPHANED_NEWSLETTER_POSTS) { |
| 73 | $this->repository->cleanupOrphanedNewsletterPosts(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 |