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:
Oliver Davies 2021-01-01 22:35:52 +00:00
parent f6ff21e04e
commit d7459ff30f
5 changed files with 80 additions and 23 deletions

View file

@ -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;
}
}

View file

@ -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(),
],
]);
}
}

View file

@ -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;
}