CommandServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 72
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 3 1
A registerModelMakeCommand() 0 4 1
A register() 0 3 1
A registerCommands() 0 13 3
A registerDbCommand() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Providers;
6
7
use LaravelFreelancerNL\Aranguent\Console\ShowCommand;
8
use LaravelFreelancerNL\Aranguent\Console\ShowModelCommand;
9
use LaravelFreelancerNL\Aranguent\Console\TableCommand;
10
use LaravelFreelancerNL\Aranguent\Console\WipeCommand;
11
use LaravelFreelancerNL\Aranguent\Console\DbCommand;
12
use Illuminate\Database\Console\DbCommand as IlluminateDbCommand;
13
use LaravelFreelancerNL\Aranguent\Console\ModelMakeCommand;
14
use Illuminate\Support\ServiceProvider;
15
16
class CommandServiceProvider extends ServiceProvider
17
{
18
    protected bool $defer = false;
19
20
    /**
21
     * The commands to be registered.
22
     *
23
     * @var string[]
24
     */
25
    protected $commands = [
26
        'ModelMake' => ModelMakeCommand::class,
27
        'Db' => DbCommand::class,
28
        'DbWipe' => WipeCommand::class,
29
        'DbShow' => ShowCommand::class,
30
        'DbTable' => TableCommand::class,
31
        'ShowModel' => ShowModelCommand::class,
32
    ];
33
34
35
    /**
36
     * Register the service provider.
37
     *
38
     * @return void
39
     */
40 498
    public function register()
41
    {
42 498
        $this->registerCommands($this->commands);
43
    }
44
45
    /**
46
     * Register the given commands.
47
     *
48
     * @param  string[]  $commands
49
     * @return void
50
     *
51
     * @SuppressWarnings("PHPMD.ElseExpression")
52
     */
53 498
    protected function registerCommands(array $commands)
54
    {
55 498
        foreach ($commands as $commandName => $command) {
56 498
            $method = "register{$commandName}Command";
57
58 498
            if (method_exists($this, $method)) {
59 498
                $this->{$method}();
60
            } else {
61 498
                $this->app->singleton($command);
62
            }
63
        }
64
65 498
        $this->commands(array_values($commands));
66
    }
67
68 498
    protected function registerDbCommand(): void
69
    {
70 498
        $this->app->extend(IlluminateDbCommand::class, function () {
71
            return new DbCommand();
72 498
        });
73
    }
74
75 498
    protected function registerModelMakeCommand(): void
76
    {
77 498
        $this->app->singleton(ModelMakeCommand::class, function ($app) {
78 498
            return new ModelMakeCommand($app['files']);
79 498
        });
80
    }
81
82
    /**
83
     * @return string[]
84
     */
85
    public function provides()
86
    {
87
        return array_values($this->commands);
88
    }
89
}
90