Skip to content

Commit

Permalink
inject dependencies (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Jan 29, 2018
1 parent a4c67f0 commit 8121e50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 27 additions & 5 deletions Command/MessageDispatchCommand.php
Expand Up @@ -2,9 +2,10 @@

namespace Happyr\Mq2phpBundle\Command;

use Happyr\Mq2phpBundle\Service\ConsumerWrapper;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -17,6 +18,28 @@ class MessageDispatchCommand extends ContainerAwareCommand
{
protected static $defaultName = 'happyr:mq2php:dispatch';

/**
* @var ConsumerWrapper
*/
private $consumer;

/**
* @var string
*/
private $secretKey;

/**
* @param ConsumerWrapper $consumer
* @param string $secretKey
*/
public function __construct(ConsumerWrapper $consumer, $secretKey)
{
$this->consumer = $consumer;
$this->secretKey = $secretKey;

parent::__construct();
}

protected function configure()
{
$this
Expand All @@ -32,15 +55,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$data = $input->getArgument('data');
$queueName = $input->getArgument('queue');
$hash = $input->getArgument('hash');
$secretKey = $this->getContainer()->getParameter('happyr.mq2php.secret_key');

if (!empty($secretKey)) {
if (!empty($this->secretKey)) {
// If we have a secret key we must validate the hash
if (!hash_equals(sha1($secretKey.$data), $hash)) {
if (!hash_equals(sha1($this->secretKey.$data), $hash)) {
throw new \Exception('Hash verification failed');
}
}

$this->getContainer()->get('happyr.mq2php.consumer_wrapper')->consume($queueName, $data);
$this->consumer->consume($queueName, $data);
}
}
3 changes: 2 additions & 1 deletion Resources/config/services.yml
Expand Up @@ -39,5 +39,6 @@ services:

happyr.mq2php.command.dispatch:
class: Happyr\Mq2phpBundle\Command\MessageDispatchCommand
arguments: ['@happyr.mq2php.consumer_wrapper', '%happyr.mq2php.secret_key%']
tags:
- { name: 'console.command', command: 'happyr:mq2php:dispatch' }
- { name: 'console.command', command: 'happyr:mq2php:dispatch' }

0 comments on commit 8121e50

Please sign in to comment.