QueuePublishCommand::sendMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 6
1
<?php
2
3
/**
4
 * Copyright 2014 Underground Elephant
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * @package     qpush-bundle
19
 * @copyright   Underground Elephant 2014
20
 * @license     Apache License, Version 2.0
21
 */
22
23
namespace Uecode\Bundle\QPushBundle\Command;
24
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
30
use Symfony\Component\DependencyInjection\ContainerInterface;
31
32
/**
33
 * @author Keith Kirk <[email protected]>
34
 */
35
class QueuePublishCommand extends Command implements ContainerAwareInterface
36
{
37
    /**
38
     * @var ContainerInterface
39
     *
40
     * @api
41
     */
42
    protected $container;
43
44
    /**
45
     * Sets the Container associated with this Controller.
46
     *
47
     * @param ContainerInterface $container A ContainerInterface instance
48
     *
49
     * @api
50
     */
51
    public function setContainer(ContainerInterface $container = null)
52
    {
53
        $this->container = $container;
54
    }
55
56
    protected $output;
57
58
    protected function configure()
59
    {
60
        $this
61
            ->setName('uecode:qpush:publish')
62
            ->setDescription('Sends a Message to a Queue')
63
            ->addArgument(
64
                'name',
65
                InputArgument::REQUIRED,
66
                'Name of the Queue'
67
            )
68
            ->addArgument(
69
                'message',
70
                InputArgument::REQUIRED,
71
                'A JSON encoded Message to send to the Queue'
72
            )
73
        ;
74
    }
75
76
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78
        $this->output = $output;
79
        $registry = $this->container->get('uecode_qpush');
80
81
        $name = $input->getArgument('name');
82
        $message = $input->getArgument('message');
83
84
        return $this->sendMessage($registry, $name, $message);
85
    }
86
87
    private function sendMessage($registry, $name, $message)
88
    {
89
        if (!$registry->has($name)) {
90
            return $this->output->writeln(
91
                sprintf("The [%s] queue you have specified does not exists!", $name)
92
            );
93
        }
94
95
        $registry->get($name)->publish(json_decode($message, true));
96
        $this->output->writeln("<info>The message has been sent.</info>");
97
98
        return 0;
99
    }
100
}
101