Passed
Push — feature/second-release ( 176745...89fc3a )
by Andrea Marco
02:27
created

EnumAnnotateCommand::handle()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 2
b 0
f 0
nc 19
nop 1
dl 0
loc 26
ccs 15
cts 15
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Commands;
6
7
use Cerbero\LaravelEnum\Enums;
8
use Cerbero\LaravelEnum\Services\Annotator;
9
use Illuminate\Console\Command;
10
use Throwable;
11
12
use function Laravel\Prompts\multiselect;
13
14
/**
15
 * The console command to annotate enums.
16
 */
17
final class EnumAnnotateCommand extends Command
18
{
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Annotate enums to ease IDE autocompletion';
25
26
    /**
27
     * The name and signature of the console command.
28
     *
29
     * @var string
30
     */
31
    protected $signature = 'enum:annotate {enums?*} {--a|all} {--f|force}';
32
33
    /**
34
     * Handle the command.
35
     */
36 4
    public function handle(Annotator $annotator): int
37
    {
38 4
        if (! $enums = $this->enums()) {
39 2
            $this->info('No enums to annotate.');
40
41 2
            return self::SUCCESS;
42
        }
43
44 2
        $e = null;
45 2
        $succeeded = true;
46
47 2
        foreach($enums as $enum) {
48
            try {
49 2
                $succeeded = $annotator->annotate($enum, !! $this->option('force')) && $succeeded;
50 1
            } catch (Throwable $e) {
51 1
                $succeeded = false;
52
            }
53
54 2
            $message = $succeeded
55 1
                ? "<bg=#a3e635;fg=#3f6212;options=bold> DONE </> {$enum}\n"
56 1
                : "<bg=#f87171;fg=#991b1b;options=bold> FAIL </> {$enum} <fg=#dc2626>{$e?->getMessage()}</>\n";
57
58 2
            $this->line($message);
59
        }
60
61 2
        return $succeeded ? self::SUCCESS : self::FAILURE;
62
    }
63
64
    /**
65
     * Retrieve the enums to annotate.
66
     *
67
     * @return list<class-string<\UnitEnum>>
68
     */
69 4
    private function enums(): array
70
    {
71 4
        if ($enums = (array) $this->argument('enums')) {
72
            /** @var list<string> $enums */
73 2
            $namespaces = array_map(fn(string $enum) => str_replace('/', '\\', $enum), $enums);
74
75
            /** @var list<class-string<\UnitEnum>> */
76 2
            return array_unique(array_filter($namespaces, 'enum_exists'));
77
        }
78
79
        /** @var list<class-string<\UnitEnum>> */
80
        return match (true) {
81 2
            empty($enums = [...Enums::namespaces()]) => [],
82 1
            $this->option('all') => $enums,
83 2
            default => multiselect('Enums to annotate:', $enums, required: true, hint: 'Press space to select.'),
84
        };
85
    }
86
}
87