Move the toTweet() functionality

Move the toTweet() functionality from the Entity into the Action.
This commit is contained in:
Oliver Davies 2021-04-21 00:19:02 +01:00
parent 9d1cbdeffd
commit 7542a2a542
2 changed files with 36 additions and 29 deletions

View file

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

View file

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