From ec6ac1d798c338968d8d0e58cc6db08e29b84092 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 4 Jan 2021 19:53:39 +0000 Subject: [PATCH] Start refactoring to use a queue References #328 --- .../PushBlogPostToSocialMedia.php | 20 +++++++++---------- .../src/Kernel/PushToSocialMediaTest.php | 12 ++++++++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php index 0aa3267..a6657d4 100644 --- a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php +++ b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -4,18 +4,18 @@ declare(strict_types=1); namespace Drupal\opdavies_blog\EventSubscriber; +use Drupal\Core\Queue\QueueFactory; use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent; use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; use Drupal\opdavies_blog\Entity\Node\Post; -use Drupal\opdavies_blog\Service\PostPusher\PostPusher; use Symfony\Component\EventDispatcher\EventSubscriberInterface; final class PushBlogPostToSocialMedia implements EventSubscriberInterface { - private PostPusher $postPusher; + private QueueFactory $queueFactory; - public function __construct(PostPusher $postPusher) { - $this->postPusher = $postPusher; + public function __construct(QueueFactory $queueFactory) { + $this->queueFactory = $queueFactory; } /** @@ -40,14 +40,12 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { return; } - if (!$this->shouldBePushed($entity)) { - return; - } + $queue = $this->queueFactory->get('opdavies_blog_push_post_to_social_media'); + $queue->createQueue(); - $this->postPusher->push($entity); - - $entity->markAsSentToSocialMedia(); - $entity->save(); + $queue->createItem([ + 'post' => $entity, + ]); } private function shouldBePushed(Post $post): bool { diff --git a/web/modules/custom/blog/tests/src/Kernel/PushToSocialMediaTest.php b/web/modules/custom/blog/tests/src/Kernel/PushToSocialMediaTest.php index 5fdd93f..d0f537a 100644 --- a/web/modules/custom/blog/tests/src/Kernel/PushToSocialMediaTest.php +++ b/web/modules/custom/blog/tests/src/Kernel/PushToSocialMediaTest.php @@ -6,6 +6,7 @@ namespace Drupal\Tests\opdavies_blog\Kernel; use Drupal\Core\Queue\QueueInterface; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; +use Drupal\opdavies_blog\Entity\Node\Post; use Drupal\Tests\node\Traits\NodeCreationTrait; final class PushToSocialMediaTest extends EntityKernelTestBase { @@ -40,6 +41,15 @@ final class PushToSocialMediaTest extends EntityKernelTestBase { ]); $this->assertSame(1, $this->queue->numberOfItems()); + + $item = $this->queue->claimItem(); + /** @var Post $post */ + $post = $item->data['post']; + + $this->assertNotNull($post); + $this->assertInstanceOf(Post::class, $post); + $this->assertSame('1', $post->id()); + $this->assertSame('Ignoring PHPCS sniffs within PHPUnit tests', $post->getTitle()); } protected function setUp() { @@ -50,7 +60,7 @@ final class PushToSocialMediaTest extends EntityKernelTestBase { $this->installSchema('node', ['node_access']); $this->queue = $this->container->get('queue') - ->get('push_post_to_social_media'); + ->get('opdavies_blog_push_post_to_social_media'); } }