Skip to content

Instantly share code, notes, and snippets.

@jverdeyen
Last active February 21, 2018 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jverdeyen/3408b1bcc1c29dbb0bdb8bc23abe7fb4 to your computer and use it in GitHub Desktop.
Save jverdeyen/3408b1bcc1c29dbb0bdb8bc23abe7fb4 to your computer and use it in GitHub Desktop.
Enqueue Symfony Console commands [QueuedCommandHandler.php]
<?php
namespace AppBundle\Service\Queue;
use AppBundle\Model\Queue\QueuedCommand;
use Enqueue\Client\ProducerInterface;
class QueuedCommandHandler
{
/**
* @var ProducerInterface
*/
protected $producer;
/**
* @var string
*/
protected $env;
/**
* @param ProducerInterface $producer
* @param string $env
*/
public function __construct(ProducerInterface $producer, $env)
{
$this->producer = $producer;
$this->env = $env;
}
public function handle(QueuedCommand $command): void
{
$argumentString = $this->createArgumentString($command->getParameters());
$fullCommand = sprintf('%s %s', $command->getName(), $argumentString);
$this->producer->sendCommand('run_command', $fullCommand);
}
public function handleString(string $fullCommand): void
{
$this->producer->sendCommand('run_command', $fullCommand);
}
private function createArgumentString(array $arguments)
{
$optionList = [];
foreach ($arguments as $key => $value) {
if (!is_int($key)) {
$optionList[] = sprintf('--%s=%s', $key, $value);
continue;
}
$optionList[] = sprintf('%s', $value);
}
$optionList[] = sprintf('--env=%s', $this->env);
return implode(' ', $optionList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment