CacheEventHandlersCommand   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 4
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 22 1
1
<?php
2
3
namespace Spatie\EventProjector\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Illuminate\Filesystem\Filesystem;
8
use Spatie\EventProjector\Projectionist;
9
use Spatie\EventProjector\EventHandlers\EventHandler;
10
11
final class CacheEventHandlersCommand extends Command
12
{
13
    protected $signature = 'event-projector:cache-event-handlers';
14
15
    protected $description = 'Cache all auto discovered event handlers';
16
17
    public function handle(Projectionist $projectionist, Filesystem $files): void
18
    {
19
        $this->info('Caching registered event handlers...');
20
21
        $projectionist->getProjectors()
22
            ->merge($projectionist->getReactors())
23
            ->map(function (EventHandler $eventHandler) {
24
                return get_class($eventHandler);
25
            })
26
            ->pipe(function (Collection $eventHandlerClasses) use ($files) {
27
                $cachePath = config('event-projector.cache_path');
28
29
                $files->makeDirectory($cachePath, 0755, true, true);
30
31
                $files->put(
32
                    $cachePath.'/event-handlers.php',
33
                    '<?php return '.var_export($eventHandlerClasses->toArray(), true).';'
34
                );
35
            });
36
37
        $this->info('All done!');
38
    }
39
}
40