Passed
Push — 166-add-dropallanalyzers-funct... ( a64239...f7932e )
by Bas
03:49
created

CommandServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 69
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\WipeCommand;
8
use LaravelFreelancerNL\Aranguent\Console\DbCommand;
9
use Illuminate\Database\Console\DbCommand as IlluminateDbCommand;
10
use LaravelFreelancerNL\Aranguent\Console\ModelMakeCommand;
11
use Illuminate\Support\ServiceProvider;
12
13
class CommandServiceProvider extends ServiceProvider
14
{
15
    protected bool $defer = false;
16
17
    /**
18
     * The commands to be registered.
19
     *
20
     * @var string[]
21
     */
22
    protected $commands = [
23
        'ModelMake' => ModelMakeCommand::class,
24
        'Db' => DbCommand::class,
25
        'DbWipe' => WipeCommand::class,
26
    ];
27
28
29
    /**
30
     * Register the service provider.
31
     *
32
     * @return void
33
     */
34 451
    public function register()
35
    {
36 451
        $this->registerCommands($this->commands);
37
    }
38
39
    /**
40
     * Register the given commands.
41
     *
42
     * @param  string[]  $commands
43
     * @return void
44
     *
45
     * @SuppressWarnings(PHPMD.ElseExpression)
46
     */
47 451
    protected function registerCommands(array $commands)
48
    {
49 451
        foreach ($commands as $commandName => $command) {
50 451
            $method = "register{$commandName}Command";
51
52 451
            if (method_exists($this, $method)) {
53 451
                $this->{$method}();
54
            } else {
55 451
                $this->app->singleton($command);
56
            }
57
        }
58
59 451
        $this->commands(array_values($commands));
60
    }
61
62 451
    protected function registerModelMakeCommand(): void
63
    {
64 451
        $this->app->singleton(ModelMakeCommand::class, function ($app) {
65 451
            return new ModelMakeCommand($app['files']);
66 451
        });
67
    }
68
69 451
    protected function registerDbCommand(): void
70
    {
71 451
        $this->app->extend(IlluminateDbCommand::class, function () {
72
            return new DbCommand();
73 451
        });
74
    }
75
76
    /**
77
     * @return string[]
78
     */
79
    public function provides()
80
    {
81
        return array_values($this->commands);
82
    }
83
}
84