Refactor
This commit is contained in:
parent
8d3f6f694e
commit
6466e6a459
3 changed files with 4 additions and 3 deletions
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_daily_emails\Action;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opd_daily_emails\Ctas;
|
||||
use Drupal\opd_daily_emails\DailyEmail;
|
||||
|
||||
readonly final class AddRandomCtaToDailyEmail {
|
||||
|
||||
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
|
||||
}
|
||||
|
||||
public function __invoke(DailyEmail $email): void {
|
||||
if (!$email->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($email->get('field_daily_email_cta')->getValue() !== []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $email->get('body')->value;
|
||||
assert(is_string($body));
|
||||
|
||||
if (str_contains(haystack: $body, needle: 'P.S.')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nodeStorage = $this->entityTypeManager->getStorage('node');
|
||||
$query = $nodeStorage->getQuery();
|
||||
$query->condition('status', NodeInterface::PUBLISHED);
|
||||
$query->condition('type', 'daily_email_cta');
|
||||
$query->accessCheck();
|
||||
$ctaNodes = $nodeStorage->loadMultiple($query->execute());
|
||||
|
||||
$ctas = Ctas::fromNodes($ctaNodes);
|
||||
|
||||
if ($ctas->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$email->set('field_daily_email_cta', $ctas->getRandomCta());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue