From d7459ff30f9ed3f62b00d40fd68124287572c4cb Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 1 Jan 2021 22:35:52 +0000 Subject: [PATCH] Extract a PostPusher service to push the post Extract a `PostPusher` service with an IFTTT implementation. That means if this needs to change to use a different service in the future, the IFTTT implementation can remain unchanged and a new implementation can be added with its own form params etc. This also makes this functionality easier to test as we can add a `NullPostPusher` implementation for testing. References #332 --- .../custom/blog/opdavies_blog.services.yml | 6 +++ .../PushBlogPostToSocialMedia.php | 28 +++---------- .../src/Service/PostPusher/HttpPostPusher.php | 17 ++++++++ .../Service/PostPusher/IftttPostPusher.php | 39 +++++++++++++++++++ .../src/Service/PostPusher/PostPusher.php | 13 +++++++ 5 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 web/modules/custom/blog/src/Service/PostPusher/HttpPostPusher.php create mode 100644 web/modules/custom/blog/src/Service/PostPusher/IftttPostPusher.php create mode 100644 web/modules/custom/blog/src/Service/PostPusher/PostPusher.php diff --git a/web/modules/custom/blog/opdavies_blog.services.yml b/web/modules/custom/blog/opdavies_blog.services.yml index 20b00ef..8dc9c65 100644 --- a/web/modules/custom/blog/opdavies_blog.services.yml +++ b/web/modules/custom/blog/opdavies_blog.services.yml @@ -10,3 +10,9 @@ services: Drupal\opdavies_blog\Repository\PostRepository: autowire: true + + Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher: + autowire: true + + Drupal\opdavies_blog\Service\PostPusher\PostPusher: + alias: Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher diff --git a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php index 9684347..0aa3267 100644 --- a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php +++ b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -4,27 +4,18 @@ declare(strict_types=1); namespace Drupal\opdavies_blog\EventSubscriber; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Config\ImmutableConfig; use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent; use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; use Drupal\opdavies_blog\Entity\Node\Post; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; +use Drupal\opdavies_blog\Service\PostPusher\PostPusher; use Symfony\Component\EventDispatcher\EventSubscriberInterface; final class PushBlogPostToSocialMedia implements EventSubscriberInterface { - private ClientInterface $client; + private PostPusher $postPusher; - private ImmutableConfig $config; - - public function __construct( - ConfigFactoryInterface $configFactory, - Client $client - ) { - $this->client = $client; - $this->config = $configFactory->get('opdavies_blog.settings'); + public function __construct(PostPusher $postPusher) { + $this->postPusher = $postPusher; } /** @@ -49,19 +40,11 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { return; } - if (!$url = $this->config->get('post_tweet_webhook_url')) { - return; - } - if (!$this->shouldBePushed($entity)) { return; } - $this->client->post($url, [ - 'form_params' => [ - 'value1' => $entity->toTweet(), - ], - ]); + $this->postPusher->push($entity); $entity->markAsSentToSocialMedia(); $entity->save(); @@ -88,4 +71,3 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { } } - diff --git a/web/modules/custom/blog/src/Service/PostPusher/HttpPostPusher.php b/web/modules/custom/blog/src/Service/PostPusher/HttpPostPusher.php new file mode 100644 index 0000000..b198122 --- /dev/null +++ b/web/modules/custom/blog/src/Service/PostPusher/HttpPostPusher.php @@ -0,0 +1,17 @@ +client = $client; + } + +} diff --git a/web/modules/custom/blog/src/Service/PostPusher/IftttPostPusher.php b/web/modules/custom/blog/src/Service/PostPusher/IftttPostPusher.php new file mode 100644 index 0000000..d5d2553 --- /dev/null +++ b/web/modules/custom/blog/src/Service/PostPusher/IftttPostPusher.php @@ -0,0 +1,39 @@ +configFactory = $configFactory; + + parent::__construct($client); + } + + public function push(Post $post): void { + $url = $this->configFactory + ->get('opdavies_blog.settings') + ->get('post_tweet_webhook_url'); + + Assert::notNull($url, 'Cannot push the post if there is no URL.'); + + $this->client->post($url, [ + 'form_params' => [ + 'value1' => $post->toTweet(), + ], + ]); + } + +} diff --git a/web/modules/custom/blog/src/Service/PostPusher/PostPusher.php b/web/modules/custom/blog/src/Service/PostPusher/PostPusher.php new file mode 100644 index 0000000..36e52ca --- /dev/null +++ b/web/modules/custom/blog/src/Service/PostPusher/PostPusher.php @@ -0,0 +1,13 @@ +