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
|
|
|
* UnsubscribeCommand. |
25
|
|
|
* |
26
|
|
|
* @author Artem Henvald <[email protected]> |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
final class UnsubscribeCommand extends AbstractCommand |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
use ArgumentChannelTrait; |
31
|
|
|
use ArgumentUserTrait; |
32
|
|
|
|
33
|
|
|
protected static $defaultName = 'centrifugo:unsubscribe'; |
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('Unsubscribe user from channel') |
53
|
|
|
->setDefinition( |
54
|
|
|
new InputDefinition([ |
55
|
|
|
new InputArgument('user', InputArgument::REQUIRED, 'User ID'), |
56
|
|
|
new InputArgument('channel', InputArgument::REQUIRED, 'Channel name'), |
57
|
|
|
]) |
58
|
|
|
) |
59
|
|
|
->setHelp( |
60
|
|
|
<<<'HELP' |
61
|
|
|
The <info>%command.name%</info> command allows to unsubscribe user from channel: |
62
|
|
|
|
63
|
|
|
<info>%command.full_name%</info> <comment>user123</comment> <comment>channelAbc</comment> |
64
|
|
|
|
65
|
|
|
Read more at https://centrifugal.github.io/centrifugo/server/http_api/#unsubscribe |
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->initializeUserArgument($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->unsubscribe($this->user, $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
|
|
|
|
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.