Skip to content

Instantly share code, notes, and snippets.

@tistre
Created November 21, 2019 14:06
Show Gist options
  • Save tistre/ce6c026dd105617237fef0319cdbeed7 to your computer and use it in GitHub Desktop.
Save tistre/ce6c026dd105617237fef0319cdbeed7 to your computer and use it in GitHub Desktop.
<?php
namespace App\Command;
use App\Module\ModuleList;
use \Exception;
use App\Event\ElvisJob;
use App\Queue\ElvisJobQueue;
use App\Queue\QueueServerConfig;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class TestQueuePublishCommand extends Command
{
/** @var string */
protected static $defaultName = 'app:test-queue-publish';
/** @var QueueServerConfig */
protected $queueConfig;
/** @var LoggerInterface */
protected $logger;
/** @var ModuleList */
protected $moduleList;
/**
* TestQueuePublishCommand constructor.
* @param QueueServerConfig $queueConfig
* @param LoggerInterface $logger
* @param ModuleList $moduleList
* @param string|null $name
*/
public function __construct(QueueServerConfig $queueConfig, LoggerInterface $logger, ModuleList $moduleList, string $name = null)
{
parent::__construct($name);
$this->queueConfig = $queueConfig;
$this->logger = $logger;
$this->moduleList = $moduleList;
}
/**
* @inheritDoc
*/
protected function configure()
{
$this
->setDescription('Test queue publish')
->setHelp('Test whether publishing to the queue is working.');
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$queue = new ElvisJobQueue($this->queueConfig, $this->logger, $this->moduleList);
$event = new ElvisJob();
$event->fromArray(
[
'type' => 'App\Module\AssetSyncCalculatedFields',
'assetId' => 'abc123'
]
);
try {
$queue->publish($event);
} catch (Exception $e) {
$output->writeln(sprintf('%s: Failed to publish message: %s', __METHOD__, $e->getMessage()));
}
try {
$queue->closeConnection();
} catch (Exception $e) {
$output->writeln(sprintf('%s: Failed to close queue: %s', __METHOD__, $e->getMessage()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment