DeleteAllCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 51 5
A __construct() 0 4 1
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\Container;
11
use JokerProject\LaravelAliyunAmqp\Entity\QueueEntity;
12
use JokerProject\LaravelAliyunAmqp\PublisherInterface;
13
14
/**
15
 * Class DeleteAllCommand
16
 *
17
 * @package JokerProject\LaravelAliyunAmqp\Commad
18
 */
19
class DeleteAllCommand extends Command
20
{
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'rabbitmq:delete-all';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Delete all queues, exchanges and binds that are defined in entities AND referenced to' .
34
    ' either a publisher or a consumer';
35
36
    /**
37
     * @var Container
38
     */
39
    private $container;
40
41
    /**
42
     * CreateEntitiesCommand constructor.
43
     *
44
     * @param Container $container
45
     */
46
    public function __construct(Container $container)
47
    {
48
        $this->container = $container;
49
        parent::__construct();
50
    }
51
52
    /**
53
     * Execute the console command.
54
     */
55
    public function handle()
56
    {
57
        $hasErrors = false;
58
        /** @var PublisherInterface $entity */
59
        foreach ($this->container->getPublishers() as $publisherName => $entity) {
60
            try {
61
                $entity->delete();
62
                $this->output->writeln(
63
                    sprintf(
64
                        "Deleted entity <info>%s</info> for publisher [<fg=yellow>%s</>]",
65
                        (string)$entity->getAliasName(),
66
                        (string)$publisherName
67
                    )
68
                );
69
            } catch (\Exception $e) {
70
                $hasErrors = true;
71
                $this->output->error(
72
                    sprintf(
73
                        "Could not delete entity %s for publisher [%s], got:\n%s",
74
                        (string)$entity->getAliasName(),
75
                        (string)$publisherName,
76
                        (string)$e->getMessage()
77
                    )
78
                );
79
            }
80
        }
81
82
        foreach ($this->container->getConsumers() as $consumerAliasName => $entity) {
83
            try {
84
                /** @var QueueEntity $entity */
85
                $entity->delete();
86
                $this->output->writeln(
87
                    sprintf(
88
                        "Deleted entity <info>%s</info> for consumer [<fg=yellow>%s</>]",
89
                        (string)$entity->getAliasName(),
90
                        (string)$consumerAliasName
91
                    )
92
                );
93
            } catch (\Exception $e) {
94
                $hasErrors = true;
95
                $this->output->error(
96
                    sprintf(
97
                        "Could not delete entity %s for consumer [%s], got:\n%s",
98
                        (string)$entity->getAliasName(),
99
                        (string)$consumerAliasName,
100
                        (string)$e->getMessage()
101
                    )
102
                );
103
            }
104
        }
105
        return (int)$hasErrors;
106
    }
107
}
108