diff --git a/web/modules/custom/blog/src/Action/ConvertPostToTweet.php b/web/modules/custom/blog/src/Action/ConvertPostToTweet.php index 4e64980..353be93 100644 --- a/web/modules/custom/blog/src/Action/ConvertPostToTweet.php +++ b/web/modules/custom/blog/src/Action/ConvertPostToTweet.php @@ -5,11 +5,45 @@ declare(strict_types=1); namespace Drupal\opdavies_blog\Action; use Drupal\opdavies_blog\Entity\Node\Post; +use Drupal\taxonomy\Entity\Term; +use Illuminate\Support\Collection; final class ConvertPostToTweet { + private Post $post; + public function __invoke(Post $post): string { - return $post->toTweet(); + $this->post = $post; + + $parts = [ + $post->label(), + $post->url('canonical', ['absolute' => TRUE]), + $this->convertTermsToHashtags(), + ]; + + return implode(PHP_EOL . PHP_EOL, $parts); + } + + private function convertTermsToHashtags(): string { + return $this->post + ->getTags() + ->filter(fn(Term $term) => !$this->tagsToRemove() + ->contains($term->label())) + ->map(fn(Term $term) => $this->convertTermToHashtag($term)) + ->implode(' '); + } + + private function tagsToRemove(): Collection { + // TODO: Move these values into configuration/settings.php. + return new Collection([ + 'Drupal Planet', + ]); + } + + private function convertTermToHashtag(Term $tag): string { + return '#' . (new Collection(explode(' ', $tag->label()))) + ->map(fn(string $word): string => ucfirst($word)) + ->implode(''); } } diff --git a/web/modules/custom/blog/src/Entity/Node/Post.php b/web/modules/custom/blog/src/Entity/Node/Post.php index 3ea094c..950aa4f 100644 --- a/web/modules/custom/blog/src/Entity/Node/Post.php +++ b/web/modules/custom/blog/src/Entity/Node/Post.php @@ -66,34 +66,7 @@ class Post extends Node implements ContentEntityBundleInterface { } public function toTweet(): string { - $parts = [ - $this->label(), - $this->url('canonical', ['absolute' => TRUE]), - $this->convertTermsToHashtags(), - ]; - - return implode(PHP_EOL . PHP_EOL, $parts); - } - - private function convertTermsToHashtags(): string { - return $this->getTags() - ->filter(fn(Term $term) => !$this->tagsToRemove() - ->contains($term->label())) - ->map(fn(Term $term) => $this->convertTermToHashtag($term)) - ->implode(' '); - } - - private function tagsToRemove(): Collection { - // TODO: Move these values into configuration/settings.php. - return new Collection([ - 'Drupal Planet', - ]); - } - - private function convertTermToHashtag(Term $tag): string { - return '#' . (new Collection(explode(' ', $tag->label()))) - ->map(fn(string $word): string => ucfirst($word)) - ->implode(''); + return ''; } }