|
| 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 | +use CakeDC\QueueMonitor\Core\DisableTrait; |
| 23 | +use Psr\Log\LogLevel; |
| 24 | +use function Cake\Collection\collection; |
| 25 | +use function Cake\I18n\__; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test Queue Command |
| 29 | + */ |
| 30 | +final class TestQueueCommand extends Command |
| 31 | +{ |
| 32 | + use DisableTrait; |
| 33 | + use MailerAwareTrait; |
| 34 | + |
| 35 | + private const ARGUMENT_EMAIL = 'email'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @inheritDoc |
| 39 | + */ |
| 40 | + public static function defaultName(): string |
| 41 | + { |
| 42 | + return 'queue-monitor test-queue'; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + public static function getDescription(): string |
| 49 | + { |
| 50 | + return __('Enqueue test email'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritDoc |
| 55 | + */ |
| 56 | + public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
| 57 | + { |
| 58 | + return parent::buildOptionParser($parser) |
| 59 | + ->setDescription(self::getDescription()) |
| 60 | + ->addArgument($this::ARGUMENT_EMAIL, [ |
| 61 | + 'help' => __('Email to send to'), |
| 62 | + 'required' => true, |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritDoc |
| 68 | + */ |
| 69 | + public function execute(Arguments $args, ConsoleIo $io) |
| 70 | + { |
| 71 | + if ($this->isDisabled()) { |
| 72 | + $this->log( |
| 73 | + 'Test Enqueue was not performed because Queue Monitor is disabled.', |
| 74 | + LogLevel::WARNING |
| 75 | + ); |
| 76 | + |
| 77 | + return self::CODE_SUCCESS; |
| 78 | + } |
| 79 | + |
| 80 | + $email = $args->getArgument(self::ARGUMENT_EMAIL); |
| 81 | + if (!Validation::email($email)) { |
| 82 | + $io->error(__('Invalid email')); |
| 83 | + |
| 84 | + return $this::CODE_ERROR; |
| 85 | + } |
| 86 | + |
| 87 | + collection(Configure::read('Queue', [])) |
| 88 | + ->each(function ( |
| 89 | + array $queueConfig, |
| 90 | + string $queueConfigKey |
| 91 | + ) use ( |
| 92 | + $email, |
| 93 | + $io |
| 94 | + ): void { |
| 95 | + /** @var \CakeDC\QueueMonitor\Mailer\TestQueueMailer $mailer */ |
| 96 | + $mailer = $this->getMailer('CakeDC/QueueMonitor.TestQueue'); |
| 97 | + /** @uses \CakeDC\QueueMonitor\Mailer\TestQueueMailer::testQueue() */ |
| 98 | + $mailer->push( |
| 99 | + action: $mailer::SEND_TEST_QUEUE, |
| 100 | + args: [ |
| 101 | + $email, |
| 102 | + $queueConfigKey, |
| 103 | + ], |
| 104 | + options: [ |
| 105 | + 'config' => $queueConfigKey, |
| 106 | + ] |
| 107 | + ); |
| 108 | + $io->info(__( |
| 109 | + 'Queued test email `{0}` in queue `{1}`', |
| 110 | + $email, |
| 111 | + $queueConfigKey |
| 112 | + )); |
| 113 | + }); |
| 114 | + |
| 115 | + return $this::CODE_SUCCESS; |
| 116 | + } |
| 117 | +} |
0 commit comments