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
This commit is contained in:
parent
f6ff21e04e
commit
d7459ff30f
|
@ -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
|
||||
|
|
|
@ -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 {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog\Service\PostPusher;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
abstract class HttpPostPusher implements PostPusher {
|
||||
|
||||
protected ClientInterface $client;
|
||||
|
||||
public function __construct(ClientInterface $client) {
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog\Service\PostPusher;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final class IftttPostPusher extends HttpPostPusher {
|
||||
|
||||
private ConfigFactoryInterface $configFactory;
|
||||
|
||||
public function __construct(
|
||||
ClientInterface $client,
|
||||
ConfigFactoryInterface $configFactory
|
||||
) {
|
||||
$this->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(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog\Service\PostPusher;
|
||||
|
||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||
|
||||
interface PostPusher {
|
||||
|
||||
public function push(Post $post): void;
|
||||
|
||||
}
|
Loading…
Reference in a new issue