ListCommand   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 72
c 1
b 0
f 0
dl 0
loc 169
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPublishersRows() 0 15 2
A buildAllRow() 0 14 1
A buildTopicsRows() 0 15 2
A handleInternal() 0 28 6
A buildQueuesRows() 0 15 2
A buildConsumersRows() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BinaryCube\CarrotMQ\Support\Laravel\Console\Commands;
6
7
use Symfony\Component\Console\Helper\TableSeparator;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Helper\TableSeparator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
use function implode;
10
use function in_array;
11
use function vsprintf;
12
use function array_merge;
13
14
/**
15
 * Class ListCommand
16
 */
17
class ListCommand extends BaseCommand
18
{
19
20
    const
21
        GROUP_ALL       = 'all',
22
        GROUP_QUEUES    = 'queues',
23
        GROUP_TOPICS    = 'topics',
24
        GROUP_CONSUMERS = 'consumers';
25
26
    /**
27
     * @var array
28
     */
29
    protected $allowedGroups = [
30
        self::GROUP_ALL,
31
        self::GROUP_QUEUES,
32
        self::GROUP_TOPICS,
33
        self::GROUP_CONSUMERS,
34
    ];
35
36
    /**
37
     * The name and signature of the console command.
38
     *
39
     * @var string
40
     */
41
    protected $signature = '
42
                            carrot-mq:list
43
                            {group=all}';
44
45
    /**
46
     * The console command description.
47
     *
48
     * @var string
49
     */
50
    protected $description = 'Listing container options like: queues, topics, consumers, publishers. Default is `all`.';
51
52
    /**
53
     * Execute the console command.
54
     *
55
     * @return integer
56
     */
57
    public function handleInternal()
58
    {
59
        $group = $this->input->getArgument('group');
60
        $group = in_array($group, $this->allowedGroups) ? $group : self::GROUP_ALL;
61
62
        $rows = [];
63
64
        switch ($group) {
65
            case self::GROUP_ALL:
66
                $rows = $this->buildAllRow();
67
                break;
68
69
            case self::GROUP_QUEUES:
70
                $rows = $this->buildQueuesRows();
71
                break;
72
73
            case self::GROUP_TOPICS:
74
                $rows = $this->buildTopicsRows();
75
                break;
76
77
            case self::GROUP_CONSUMERS:
78
                $rows = $this->buildConsumersRows();
79
                break;
80
        }
81
82
        $this->table(['Group', 'Values'], $rows);
83
84
        return 0;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    protected function buildAllRow()
91
    {
92
        $separator = new TableSeparator();
93
94
        return (
95
            array_merge(
96
                [],
97
                $this->buildQueuesRows(),
98
                [$separator],
99
                $this->buildTopicsRows(),
100
                [$separator],
101
                $this->buildConsumersRows(),
102
                [$separator],
103
                $this->buildPublishersRows()
104
            )
105
        );
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    protected function buildQueuesRows()
112
    {
113
        $result = [];
114
        $items  = [];
115
116
        foreach ($this->carrot->container()->queues()->all() as $name => $queue) {
117
            $items[] = vsprintf('%s (%s)', [$name, $queue->name()]);
118
        }
119
120
        $result[] = [
121
            'queues',
122
            implode(PHP_EOL, $items),
123
        ];
124
125
        return $result;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    protected function buildTopicsRows()
132
    {
133
        $result = [];
134
        $items  = [];
135
136
        foreach ($this->carrot->container()->topics()->all() as $name => $topic) {
137
            $items[] = vsprintf('%s (%s)', [$name, $topic->name()]);
138
        }
139
140
        $result[] = [
141
            'topics',
142
            implode(PHP_EOL, $items),
143
        ];
144
145
        return $result;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    protected function buildConsumersRows()
152
    {
153
        $result = [];
154
        $items  = [];
155
156
        foreach ($this->carrot->container()->consumers()->all() as $name => $consumer) {
157
            $items[] = vsprintf('%s', [$name]);
158
        }
159
160
        $result[] = [
161
            'consumers',
162
            implode(PHP_EOL, $items),
163
        ];
164
165
        return $result;
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    protected function buildPublishersRows()
172
    {
173
        $result = [];
174
        $items  = [];
175
176
        foreach ($this->carrot->container()->publishers()->all() as $name => $consumer) {
177
            $items[] = vsprintf('%s', [$name]);
178
        }
179
180
        $result[] = [
181
            'publishers',
182
            implode(PHP_EOL, $items),
183
        ];
184
185
        return $result;
186
    }
187
188
}
189