CacheEventHandlersCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 2
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