client = $client; $this->config = $configFactory->get('opdavies_blog.settings'); } /** * @inheritDoc */ public static function getSubscribedEvents() { return [ HookEventDispatcherInterface::ENTITY_INSERT => 'onEntityUpdate', HookEventDispatcherInterface::ENTITY_UPDATE => 'onEntityUpdate', ]; } public function onEntityUpdate(AbstractEntityEvent $event): void { $entity = $event->getEntity(); if ($entity->getEntityTypeId() != 'node') { return; } /** @var Post $entity */ if ($entity->bundle() != 'post') { 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(), ], ]); $entity->markAsSentToSocialMedia(); $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; } }