Completed
Push — master ( b2e1c0...ab8e7d )
by Artem
01:13
created

Command/PublishCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of the FreshCentrifugoBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\CentrifugoBundle\Command;
14
15
use Fresh\CentrifugoBundle\Service\Centrifugo;
16
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputDefinition;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
23
/**
24
 * PublishCommand.
25
 *
26
 * @author Artem Henvald <[email protected]>
27
 */
28 View Code Duplication
final class PublishCommand extends AbstractCommand
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
{
30
    use ArgumentDataTrait;
31
    use ArgumentChannelTrait;
32
33
    protected static $defaultName = 'centrifugo:publish';
34
35
    /**
36
     * @param Centrifugo        $centrifugo
37
     * @param CentrifugoChecker $centrifugoChecker
38
     */
39
    public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
40
    {
41
        $this->centrifugoChecker = $centrifugoChecker;
42
43
        parent::__construct($centrifugo);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function configure(): void
50
    {
51
        $this
52
            ->setDescription('Publish data into channel')
53
            ->setDefinition(
54
                new InputDefinition([
55
                    new InputArgument('data', InputArgument::REQUIRED, 'Data in JSON format'),
56
                    new InputArgument('channel', InputArgument::REQUIRED, 'Channel name'),
57
                ])
58
            )
59
            ->setHelp(
60
                <<<'HELP'
61
The <info>%command.name%</info> command allows to publish data into channel:
62
63
<info>%command.full_name%</info> <comment>'{"foo":"bar"}'</comment> <comment>channelAbc</comment>
64
65
Read more at https://centrifugal.github.io/centrifugo/server/http_api/#publish
66
HELP
67
            )
68
        ;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function initialize(InputInterface $input, OutputInterface $output): void
75
    {
76
        parent::initialize($input, $output);
77
78
        $this->initializeDataArgument($input);
79
        $this->initializeChannelArgument($input);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function execute(InputInterface $input, OutputInterface $output): int
86
    {
87
        $io = new SymfonyStyle($input, $output);
88
89
        try {
90
            $this->centrifugo->publish($this->data, $this->channel);
91
            $io->success('DONE');
92
        } catch (\Throwable $e) {
93
            $io->error($e->getMessage());
94
95
            return $e->getCode();
96
        }
97
98
        return 0;
99
    }
100
}
101