Completed
Push — master ( 00f04e...1d93d6 )
by Freek
03:46
created

CacheEventHandlersCommand   A

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
<?php
2
3
namespace Spatie\EventSourcing\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use Spatie\EventSourcing\EventHandlers\EventHandler;
9
use Spatie\EventSourcing\Projectionist;
10
11
final class CacheEventHandlersCommand extends Command
12
{
13
    protected $signature = 'event-sourcing: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(fn(EventHandler $eventHandler) => get_class($eventHandler))
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting ',' or ')'
Loading history...
24
            ->pipe(function (Collection $eventHandlerClasses) use ($files) {
25
                $cachePath = config('event-sourcing.cache_path');
26
27
                $files->makeDirectory($cachePath, 0755, true, true);
28
29
                $files->put(
30
                    $cachePath.'/event-handlers.php',
31
                    '<?php return '.var_export($eventHandlerClasses->toArray(), true).';'
32
                );
33
            });
34
35
        $this->info('All done!');
36
    }
37
}
38