|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\os2loop_mail_notifications\Command; |
| 6 | + |
| 7 | +use Drupal\Component\Utility\NestedArray; |
| 8 | +use Drupal\Core\Mail\MailManagerInterface; |
| 9 | +use Drupal\node\Entity\Node; |
| 10 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 13 | +use Symfony\Component\Console\Input\InputArgument; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Input\InputOption; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 18 | +use Symfony\Component\Yaml\Yaml; |
| 19 | + |
| 20 | +// phpcs:disable Drupal.Commenting.ClassComment.Missing |
| 21 | +#[AsCommand( |
| 22 | + name: 'os2loop_mail_notifications:mail:send', |
| 23 | + description: 'Send a mail', |
| 24 | + aliases: ['example'], |
| 25 | +)] |
| 26 | +final class SendMailCommand extends Command { |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + private readonly MailManagerInterface $mailManager, |
| 30 | + ) { |
| 31 | + parent::__construct(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + protected function configure() { |
| 38 | + $this |
| 39 | + ->addArgument('to', InputArgument::REQUIRED, 'The mail recipient') |
| 40 | + ->addOption('module', NULL, InputOption::VALUE_REQUIRED, 'The module', 'os2loop_flag_content') |
| 41 | + ->addOption('key', NULL, InputOption::VALUE_REQUIRED, 'The key', 'flag_content') |
| 42 | + ->addOption('langcode', NULL, InputOption::VALUE_REQUIRED, 'The langcode', 'en') |
| 43 | + ->addOption('params', NULL, InputOption::VALUE_REQUIRED, 'The params (whatever that may be) as a Yaml object', '{}') |
| 44 | + ->setHelp(<<<HELP |
| 45 | +Examples: |
| 46 | +
|
| 47 | +drush %command.name% test@example.com --params ' |
| 48 | +message: My test message |
| 49 | +' |
| 50 | +HELP); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 57 | + $io = new SymfonyStyle($input, $output); |
| 58 | + |
| 59 | + $to = $input->getArgument('to'); |
| 60 | + $module = $input->getOption('module'); |
| 61 | + $key = $input->getOption('key'); |
| 62 | + $langcode = $input->getOption('langcode'); |
| 63 | + try { |
| 64 | + $params = Yaml::parse($input->getOption('params')); |
| 65 | + } |
| 66 | + catch (\Exception $e) { |
| 67 | + throw new InvalidArgumentException(dt('Invalid params: %message', ['%message' => $e->getMessage()])); |
| 68 | + } |
| 69 | + |
| 70 | + $defaultParams = match ($module) { |
| 71 | + 'os2loop_flag_content' => [ |
| 72 | + 'reason' => 'reason', |
| 73 | + 'message' => __FILE__, |
| 74 | + 'node' => Node::create([ |
| 75 | + 'type' => 'test', |
| 76 | + 'nid' => 0, |
| 77 | + 'title' => __FILE__, |
| 78 | + ]), |
| 79 | + ], |
| 80 | + default => throw new InvalidArgumentException(dt('Unknown module: %module', ['%module' => $module])), |
| 81 | + }; |
| 82 | + |
| 83 | + $params = NestedArray::mergeDeep($defaultParams, (array) $params); |
| 84 | + |
| 85 | + $send = TRUE; |
| 86 | + $result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send); |
| 87 | + |
| 88 | + $success = $result['result'] ?? FALSE; |
| 89 | + if ($success) { |
| 90 | + $io->success(dt('Mail successfully sent to %to', ['%to' => $to])); |
| 91 | + } |
| 92 | + else { |
| 93 | + $io->error(dt('Error sending mail to %to', ['%to' => $to])); |
| 94 | + } |
| 95 | + |
| 96 | + // Show the message data. |
| 97 | + $io->writeln(Yaml::dump($result)); |
| 98 | + |
| 99 | + return $success ? self::SUCCESS : self::FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments