oliverdavies.uk/web/modules/custom/blog/src/Service/PostPusher/IftttPostPusher.php
Oliver Davies 2a0a2d47fa Rename Action to UseCase
Rename to avoid confusion with Drupal's core Action type.
2021-05-24 22:31:01 +01:00

46 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Drupal\opdavies_blog\Service\PostPusher;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\opdavies_blog\UseCase\ConvertPostToTweet;
use GuzzleHttp\ClientInterface;
use Webmozart\Assert\Assert;
final class IftttPostPusher extends WebhookPostPusher {
use StringTranslationTrait;
private ConfigFactoryInterface $configFactory;
public function __construct(
ConvertPostToTweet $convertPostToTweet,
ClientInterface $client,
ConfigFactoryInterface $configFactory
) {
$this->convertPostToTweet = $convertPostToTweet;
$this->configFactory = $configFactory;
parent::__construct($convertPostToTweet, $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' => $this->t('Blogged: @text', ['@text' => ($this->convertPostToTweet)($post)])
->render(),
],
]);
}
}