Last active
February 21, 2018 15:31
-
-
Save jverdeyen/3408b1bcc1c29dbb0bdb8bc23abe7fb4 to your computer and use it in GitHub Desktop.
Enqueue Symfony Console commands [QueuedCommandHandler.php]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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