MessageDispatchCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 52
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 9 1
A execute() 0 15 3
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