Completed
Push — master ( a4c67f...8121e5 )
by Tobias
02:01
created

MessageDispatchCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Happyr\Mq2phpBundle\Command;
4
5
use Happyr\Mq2phpBundle\Service\ConsumerWrapper;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * A command that will be called by dispatch-message.php.
13
 * It will give a SimpleBus message envelope to the ConsumerWrapper.
14
 *
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
class MessageDispatchCommand extends ContainerAwareCommand
18
{
19
    protected static $defaultName = 'happyr:mq2php:dispatch';
20
21
    /**
22
     * @var ConsumerWrapper
23
     */
24
    private $consumer;
25
26
    /**
27
     * @var string
28
     */
29
    private $secretKey;
30
31
    /**
32
     * @param ConsumerWrapper $consumer
33
     * @param string          $secretKey
34
     */
35
    public function __construct(ConsumerWrapper $consumer, $secretKey)
36
    {
37
        $this->consumer = $consumer;
38
        $this->secretKey = $secretKey;
39
40
        parent::__construct();
41
    }
42
43
    protected function configure()
44
    {
45
        $this
46
            ->setDescription('Dispatch a message from a queue to simple bus')
47
            ->addArgument('queue', InputArgument::REQUIRED, 'The name of the queue')
48
            ->addArgument('data', InputArgument::REQUIRED, 'A serialized event to dispatch')
49
            ->addArgument('hash', InputArgument::OPTIONAL, 'A hash that could be used to verify the message is valid')
50
        ;
51
    }
52
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $data = $input->getArgument('data');
56
        $queueName = $input->getArgument('queue');
57
        $hash = $input->getArgument('hash');
58
59
        if (!empty($this->secretKey)) {
60
            // If we have a secret key we must validate the hash
61
            if (!hash_equals(sha1($this->secretKey.$data), $hash)) {
62
                throw new \Exception('Hash verification failed');
63
            }
64
        }
65
66
        $this->consumer->consume($queueName, $data);
67
    }
68
}
69