MakeAggregateCommand::handle()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 30
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 48
rs 9.1288
1
<?php
2
3
namespace Chocofamily\LaravelEventSauce\Console;
4
5
use Chocofamily\LaravelEventSauce\Exceptions\MakeFileFailed;
6
use DateTimeImmutable;
7
use Illuminate\Support\Str;
8
9
final class MakeAggregateCommand extends MakeCommand
10
{
11
    protected $signature = 'make:aggregate-root {namespace} {--migration}';
12
13
    protected $description = 'Create a new aggregate root and resources';
14
15
    public function handle(): void
16
    {
17
        /** @var string $namespace */
18
        $namespace = $this->argument('namespace');
19
        /** @scrutinizer ignore-type */
20
        $aggregateRootClass = $this->formatClassName($namespace);
21
22
        $aggregateRootPath = $this->getPath($aggregateRootClass);
23
24
        $aggregateRootIdClass = $this->formatClassName($namespace.'Id');
25
26
        $aggregateRootIdPath = $this->getPath($aggregateRootIdClass);
27
28
        $aggregateRootRepositoryClass = $this->formatClassName($namespace.'Repository');
29
        $aggregateRootRepositoryPath = $this->getPath($aggregateRootRepositoryClass);
30
31
        try {
32
            $this->ensureValidPaths([
33
                $aggregateRootPath,
34
                $aggregateRootIdPath,
35
                $aggregateRootRepositoryPath,
36
            ]);
37
        } catch (MakeFileFailed $exception) {
38
            $this->error($exception->getMessage());
39
        }
40
        $this->makeDirectory($aggregateRootPath);
41
42
        $replacements = [
43
            'aggregateRoot' => $aggregateRoot = class_basename($aggregateRootClass),
44
            'namespace' => substr($aggregateRootClass, 0, strrpos($aggregateRootClass, '\\')),
45
            'table' => $this->option('migration') ? Str::snake(class_basename($aggregateRootClass)).'_domain_messages' : config('eventsauce.table'),
46
            'migration' => 'Create'.ucfirst(class_basename($aggregateRootClass)).'DomainMessagesTable',
47
        ];
48
49
        $this->makeFiles([
50
            'AggregateRoot' => $aggregateRootPath,
51
            'AggregateRootId' => $aggregateRootIdPath,
52
            'AggregateRootRepository' => $aggregateRootRepositoryPath,
53
        ], $replacements);
54
55
        if ($this->option('migration')) {
56
            $this->createMigration($replacements);
57
        }
58
59
        $this->info("{$aggregateRoot} classes and resources created successfully!");
60
61
        if ($this->option('migration')) {
62
            $this->comment("Run `php artisan migrate` to create the {$replacements['table']} table.");
63
        }
64
    }
65
66
    private function createMigration(array $replacements): void
67
    {
68
        $timestamp = (new DateTimeImmutable())->format('Y_m_d_His');
69
        $filename = "{$timestamp}_create_{$replacements['table']}_table.php";
70
71
        $this->filesystem->put(
72
            $this->laravel->databasePath("migrations/{$filename}"),
73
            $this->getStubContent('create_domain_messages_table', $replacements)
74
        );
75
    }
76
}
77