Refactor sending posts to social media

This commit is contained in:
Oliver Davies 2020-08-28 12:35:00 +01:00
parent 59a98b8d78
commit 92f1c0f84c
2 changed files with 19 additions and 5 deletions

View file

@ -1,5 +1,6 @@
services:
Drupal\opdavies_blog\EventSubscriber\PushBlogPostToSocialMedia:
autowire: true
tags:
- { name: event_subscriber }

View file

@ -4,13 +4,29 @@ declare(strict_types=1);
namespace Drupal\opdavies_blog\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\opdavies_blog\Entity\Node\Post;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
private ClientInterface $client;
private ImmutableConfig $config;
public function __construct(
ConfigFactoryInterface $configFactory,
Client $client
) {
$this->client = $client;
$this->config = $configFactory->get('opdavies_talks.config');
}
/**
* @inheritDoc
*/
@ -45,14 +61,11 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
return;
}
$url = \Drupal::configFactory()->get('opdavies_talks.config')
->get('zapier_post_tweet_url');
if (!$url) {
if (!$url = $this->config->get('zapier_post_tweet_url')) {
return;
}
\Drupal::httpClient()->post($url, [
$this->client->post($url, [
'form_params' => [
'message' => $entity->toTweet(),
],