Test Failed
Pull Request — stable (#129)
by
unknown
02:21
created

EventGenerateCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
A makeEventAndListeners() 0 10 2
A makeListeners() 0 10 2
1
<?php
2
3
namespace LaravelZero\Framework\Commands\Component\Illuminate\Events;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
8
class EventGenerateCommand extends Command
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'event:generate';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Generate the missing events and listeners based on registration';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return void
28
     */
29
    public function handle()
30
    {
31
        $class = $this->laravel->getNamespace();
32
        $class = $class . '\\Providers\\EventServiceProvider';
33
        $provider = $this->laravel->make($class, ['app' => $this->getApplication()]);
34
35
        foreach ($provider->listens() as $event => $listeners) {
36
            $this->makeEventAndListeners($event, $listeners);
37
        }
38
39
        $this->info('Events and listeners generated successfully!');
40
    }
41
42
    /**
43
     * Make the event and listeners for the given event.
44
     *
45
     * @param  string  $event
46
     * @param  array  $listeners
47
     * @return void
48
     */
49
    protected function makeEventAndListeners($event, $listeners)
50
    {
51
        if (! Str::contains($event, '\\')) {
52
            return;
53
        }
54
55
        $this->callSilent('make:event', ['name' => $event]);
56
57
        $this->makeListeners($event, $listeners);
58
    }
59
60
    /**
61
     * Make the listeners for the given event.
62
     *
63
     * @param  string  $event
64
     * @param  array  $listeners
65
     * @return void
66
     */
67
    protected function makeListeners($event, $listeners)
68
    {
69
        foreach ($listeners as $listener) {
70
            $listener = preg_replace('/@.+$/', '', $listener);
71
72
            $this->callSilent('make:listener', array_filter(
73
                ['name' => $listener, '--event' => $event]
74
            ));
75
        }
76
    }
77
}
78