PublishCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 73
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A configure() 21 21 1
A initialize() 7 7 1
A execute() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\CentrifugoChecker;
16
use Fresh\CentrifugoBundle\Service\CentrifugoInterface;
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
Duplication introduced by
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 ArgumentChannelTrait;
31
    use ArgumentDataTrait;
32
33
    protected static $defaultName = 'centrifugo:publish';
34
35
    /**
36
     * @param CentrifugoInterface $centrifugo
37
     * @param CentrifugoChecker   $centrifugoChecker
38
     */
39
    public function __construct(CentrifugoInterface $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