|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | +namespace CakeDC\QueueMonitor\Command; |
| 14 | + |
| 15 | +use Cake\Command\Command; |
| 16 | +use Cake\Console\Arguments; |
| 17 | +use Cake\Console\ConsoleIo; |
| 18 | +use Cake\Console\ConsoleOptionParser; |
| 19 | +use Cake\Core\Configure; |
| 20 | +use Cake\Mailer\MailerAwareTrait; |
| 21 | +use Cake\Validation\Validation; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test Enqueue Command |
| 25 | + */ |
| 26 | +final class TestEnqueueCommand extends Command |
| 27 | +{ |
| 28 | + use MailerAwareTrait; |
| 29 | + |
| 30 | + private const ARGUMENT_EMAIL = 'email'; |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritDoc |
| 34 | + */ |
| 35 | + public static function defaultName(): string |
| 36 | + { |
| 37 | + return 'queue-monitor test-enqueue'; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public static function getDescription(): string |
| 44 | + { |
| 45 | + return __('Enqueue test email'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritDoc |
| 50 | + */ |
| 51 | + public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
| 52 | + { |
| 53 | + return parent::buildOptionParser($parser) |
| 54 | + ->setDescription(__('Enqueue test email')) |
| 55 | + ->addArgument($this::ARGUMENT_EMAIL, [ |
| 56 | + 'help' => __('Email to send to'), |
| 57 | + 'required' => true, |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritDoc |
| 63 | + */ |
| 64 | + public function execute(Arguments $args, ConsoleIo $io) |
| 65 | + { |
| 66 | + $email = $args->getArgument(self::ARGUMENT_EMAIL); |
| 67 | + if (!Validation::email($email)) { |
| 68 | + $io->error(__('Invalid email')); |
| 69 | + |
| 70 | + return $this::CODE_ERROR; |
| 71 | + } |
| 72 | + |
| 73 | + collection(Configure::read('Queue', [])) |
| 74 | + ->each(function ( |
| 75 | + array $queueConfig, |
| 76 | + string $queueConfigKey |
| 77 | + ) use ( |
| 78 | + $email, |
| 79 | + $io |
| 80 | + ): void { |
| 81 | + /** @var \CakeDC\QueueMonitor\Mailer\TestEnqueueMailer $mailer */ |
| 82 | + $mailer = $this->getMailer('QueueMonitor.TestEnqueue'); |
| 83 | + /** @uses \CakeDC\QueueMonitor\Mailer\TestEnqueueMailer::testEnqueue() */ |
| 84 | + $mailer->push( |
| 85 | + action: $mailer::SEND_TEST_ENQUEUE, |
| 86 | + args: [ |
| 87 | + $email, |
| 88 | + $queueConfigKey, |
| 89 | + ], |
| 90 | + options: [ |
| 91 | + 'config' => $queueConfigKey, |
| 92 | + ] |
| 93 | + ); |
| 94 | + $io->info(__( |
| 95 | + 'Enqueued test email `{0}` in queue `{1}`', |
| 96 | + $email, |
| 97 | + $queueConfigKey |
| 98 | + )); |
| 99 | + }); |
| 100 | + |
| 101 | + return $this::CODE_SUCCESS; |
| 102 | + } |
| 103 | +} |
0 commit comments