MakeProjectorCommand::rewriteToSyncProjector()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Symfony\Component\Console\Input\InputOption;
7
8
final class MakeProjectorCommand extends GeneratorCommand
9
{
10
    protected $name = 'make:projector';
11
12
    protected $description = 'Create a new projector';
13
14
    protected $type = 'Projector';
15
16
    public function handle()
17
    {
18
        parent::handle();
19
20
        if (! $this->option('queued')) {
21
            return;
22
        }
23
24
        $this->rewriteToSyncProjector();
25
    }
26
27
    protected function getStub()
28
    {
29
        return __DIR__.'/../../stubs/projector.stub';
30
    }
31
32
    protected function getDefaultNamespace($rootNamespace)
33
    {
34
        return $rootNamespace.'\Projectors';
35
    }
36
37
    protected function rewriteToSyncProjector()
38
    {
39
        $name = $this->qualifyClass($this->getNameInput());
40
41
        $path = $this->getPath($name);
42
43
        $content = file_get_contents($path);
44
45
        $content = str_replace('implements Projector', 'implements QueuedProjector', $content);
46
        $content = str_replace('use Spatie\EventProjector\Projectors\Projector;', 'use Spatie\EventProjector\Projectors\QueuedProjector;', $content);
47
48
        file_put_contents($path, $content);
49
    }
50
51
    protected function getOptions()
52
    {
53
        return [
54
            ['queued', 'Q', InputOption::VALUE_NONE, 'Create a QueuedProjector'],
55
        ];
56
    }
57
}
58