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
5 changed files with 80 additions and 23 deletions
|
@ -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 {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue