SetupCommand::handle()   B
last analyzed

Complexity

Conditions 9
Paths 144

Size

Total Lines 90
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 60
nc 144
nop 0
dl 0
loc 90
rs 7.0238
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Author: Joker
4
 * Date: 2020-05-08 13:57
5
 */
6
7
namespace JokerProject\LaravelAliyunAmqp\Command;
8
9
use Illuminate\Console\Command;
10
use JokerProject\LaravelAliyunAmqp\ConsumerInterface;
11
use JokerProject\LaravelAliyunAmqp\Container;
12
use JokerProject\LaravelAliyunAmqp\Entity\AMQPEntityInterface;
13
use JokerProject\LaravelAliyunAmqp\Entity\ExchangeEntity;
14
use JokerProject\LaravelAliyunAmqp\Entity\QueueEntity;
15
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
16
17
/**
18
 * Class SetupCommand
19
 *
20
 * @package JokerProject\LaravelAliyunAmqp\Commad
21
 */
22
class SetupCommand extends Command
23
{
24
    /**
25
     * The name and signature of the console command.
26
     *
27
     * @var string
28
     */
29
    protected $signature = 'rabbitmq:setup {--force}';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create all queues, exchanges and binds that are defined in entities AND referenced to' .
37
        ' either a publisher or a consumer';
38
39
    /**
40
     * @var Container
41
     */
42
    private $container;
43
44
    /**
45
     * CreateEntitiesCommand constructor.
46
     *
47
     * @param Container $container
48
     */
49
    public function __construct(Container $container)
50
    {
51
        $this->container = $container;
52
        parent::__construct();
53
    }
54
55
    /**
56
     * @param AMQPEntityInterface $entity
57
     * @param string $type
58
     * @param string $resourceName
59
     * @param bool $forceRecreate
60
     */
61
    private function createEntity(
62
        AMQPEntityInterface $entity,
63
        string $type,
64
        string $resourceName,
65
        bool $forceRecreate = false
66
    ) {
67
        if (true === $forceRecreate) {
68
            $this->output->writeln(
69
                sprintf(
70
                    "Deleting <info>%s</info> <fg=yellow>%s</>",
71
                    (string)($entity instanceof QueueEntity) ? 'QUEUE' : 'EXCHANGE',
72
                    (string)$entity->getAliasName()
73
                )
74
            );
75
            $entity->delete();
76
        }
77
78
        $entity->create();
79
        $this->output->writeln(
80
            sprintf(
81
                "Created <info>%s</info> <fg=yellow>%s</> for %s [<fg=yellow>%s</>]",
82
                (string)($entity instanceof QueueEntity) ? 'QUEUE' : 'EXCHANGE',
83
                (string)$entity->getAliasName(),
84
                (string)$type,
85
                (string)$resourceName
86
            )
87
        );
88
    }
89
90
    /**
91
     * Execute the console command.
92
     */
93
    public function handle()
94
    {
95
        $forceRecreate = $this->input->getOption('force');
96
97
        $hasErrors = false;
98
        /** @var QueueEntity|ExchangeEntity $entity */
99
        foreach ($this->container->getPublishers() as $publisherName => $entity) {
100
            try {
101
                $this->createEntity($entity, 'publisher', $publisherName, $forceRecreate);
102
            } catch (AMQPProtocolChannelException $e) {
103
                $hasErrors = true;
104
                $this->output->error(
105
                    sprintf(
106
                        "Could not create entity %s for publisher [%s], got:\n%s",
107
                        (string)$entity->getAliasName(),
108
                        (string)$publisherName,
109
                        (string)$e->getMessage()
110
                    )
111
                );
112
                $entity->reconnect();
113
            }
114
        }
115
116
        /** @var QueueEntity|ExchangeEntity $entity */
117
        foreach ($this->container->getConsumers() as $publisherName => $entity) {
118
            try {
119
                $this->createEntity($entity, 'consumer', $publisherName, $forceRecreate);
120
            } catch (AMQPProtocolChannelException $e) {
121
                $hasErrors = true;
122
                $this->output->error(
123
                    sprintf(
124
                        "Could not create entity %s for consumer [%s], got:\n%s",
125
                        (string)$entity->getAliasName(),
126
                        (string)$publisherName,
127
                        (string)$e->getMessage()
128
                    )
129
                );
130
                $entity->reconnect();
131
            }
132
        }
133
134
        $this->output->block("Create binds");
135
        /** @var PublisherInterface $entity */
136
        foreach ($this->container->getPublishers() as $publisherName => $entity) {
137
            try {
138
                $entity->bind();
139
                $this->output->writeln(
140
                    sprintf(
141
                        "Created bind <info>%s</info> for publisher [<fg=yellow>%s</>]",
142
                        (string)$entity->getAliasName(),
143
                        (string)$publisherName
144
                    )
145
                );
146
            } catch (\Exception $e) {
147
                $hasErrors = true;
148
                $this->output->error(
149
                    sprintf(
150
                        "Could not bind entity %s for publisher [%s], got:\n%s",
151
                        (string)$entity->getAliasName(),
152
                        (string)$publisherName,
153
                        (string)$e->getMessage()
154
                    )
155
                );
156
            }
157
        }
158
159
        /** @var ConsumerInterface $entity */
160
        foreach ($this->container->getConsumers() as $consumerAliasName => $entity) {
161
            try {
162
                $entity->bind();
163
                $this->output->writeln(
164
                    sprintf(
165
                        "Bind entity <info>%s</info> for consumer [<fg=yellow>%s</>]",
166
                        (string)$entity->getAliasName(),
167
                        (string)$consumerAliasName
168
                    )
169
                );
170
            } catch (\Exception $e) {
171
                $hasErrors = true;
172
                $this->output->error(
173
                    sprintf(
174
                        "Could not create bind %s for consumer [%s], got:\n%s",
175
                        (string)$entity->getAliasName(),
176
                        (string)$consumerAliasName,
177
                        (string)$e->getMessage()
178
                    )
179
                );
180
            }
181
        }
182
        return (int)$hasErrors;
183
    }
184
}
185