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 Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* DisconnectCommand. |
23
|
|
|
* |
24
|
|
|
* @author Artem Henvald <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class DisconnectCommand extends AbstractCommand |
27
|
|
|
{ |
28
|
|
|
use ArgumentUserTrait; |
29
|
|
|
|
30
|
|
|
protected static $defaultName = 'centrifugo:disconnect'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
protected function configure(): void |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setDescription('Disconnect user by ID') |
39
|
|
|
->setDefinition( |
40
|
|
|
new InputDefinition([ |
41
|
|
|
new InputArgument('user', InputArgument::REQUIRED, 'User ID'), |
42
|
|
|
]) |
43
|
|
|
) |
44
|
|
|
->setHelp( |
45
|
|
|
<<<'HELP' |
46
|
|
|
The <info>%command.name%</info> command allows to disconnect user by ID: |
47
|
|
|
|
48
|
|
|
<info>%command.full_name%</info> <comment>user123</comment> |
49
|
|
|
|
50
|
|
|
Read more at https://centrifugal.github.io/centrifugo/server/http_api/#disconnect |
51
|
|
|
HELP |
52
|
|
|
) |
53
|
|
|
; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output): void |
60
|
|
|
{ |
61
|
|
|
parent::initialize($input, $output); |
62
|
|
|
|
63
|
|
|
$this->initializeUserArgument($input); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
70
|
|
|
{ |
71
|
|
|
$io = new SymfonyStyle($input, $output); |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$this->centrifugo->disconnect($this->user); |
75
|
|
|
$io->success('DONE'); |
76
|
|
|
} catch (\Throwable $e) { |
77
|
|
|
$io->error($e->getMessage()); |
78
|
|
|
|
79
|
|
|
return $e->getCode(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return 0; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.