From a6f378294c61e29a4832b4e3c42eec8d766f1cf7 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 1 Jan 2021 21:50:30 +0000 Subject: [PATCH] Extract a helper method to group checks [ci skip] --- .../PushBlogPostToSocialMedia.php | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php index d3e94d6..a50ac13 100644 --- a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php +++ b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -49,27 +49,14 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { return; } - if (!$entity->isPublished()) { - return; - } - - if (!$entity->shouldSendToSocialMedia()) { - return; - } - - // If this post has already been sent to social media, do not send it again. - if ($entity->hasBeenSentToSocialMedia()) { - return; - } - - if ($entity->isExternalPost()) { - return; - } - if (!$url = $this->config->get('post_tweet_webhook_url')) { return; } + if (!$this->shouldBePushed($entity)) { + return; + } + $this->client->post($url, [ 'form_params' => [ 'message' => $entity->toTweet(), @@ -80,4 +67,25 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { $entity->save(); } + private function shouldBePushed(Post $post): bool { + if ($post->isExternalPost()) { + return FALSE; + } + + if (!$post->isPublished()) { + return FALSE; + } + + if (!$post->shouldSendToSocialMedia()) { + return FALSE; + } + + if ($post->hasBeenSentToSocialMedia()) { + return FALSE; + } + + return TRUE; + } + } +