DeleteAllCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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