CommandsServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 91
Duplicated Lines 35.16 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 32
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCommand() 0 6 1
B registerCommands() 32 59 1
A __invoke() 0 21 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Schnittstabil\Dartisan\ServiceProviders;
4
5
use Garden\Cli\Args;
6
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
7
use Illuminate\Database\Migrations\MigrationCreator;
8
use Illuminate\Database\Migrations\Migrator;
9
use Schnittstabil\Dartisan\Commands\MigrateCommand;
10
use Schnittstabil\Dartisan\Commands\MigrateInstallCommand;
11
use Schnittstabil\Dartisan\Commands\MigrateMakeCommand;
12
use Schnittstabil\Dartisan\Commands\MigrateResetCommand;
13
use Schnittstabil\Dartisan\Commands\MigrateRollbackCommand;
14
use Schnittstabil\Dartisan\Commands\MigrateStatusCommand;
15
use Schnittstabil\Dartisan\Container;
16
use Schnittstabil\Dartisan\OutputInterface;
17
18
/**
19
 * @SuppressWarnings(PHPMD.ShortVariable)
20
 */
21
class CommandsServiceProvider
22
{
23
    protected function registerCommand(Container $container, $class, callable $factory)
24
    {
25
        $container->set($class, $factory);
26
27
        return ['name' => $class::$name, 'class' => $class];
28
    }
29
30
    protected function registerCommands(Container $container)
31
    {
32
        $commands = [];
33
34 View Code Duplication
        $commands[] = $this->registerCommand($container, MigrateCommand::class, function (Container $c) {
35
            return new MigrateCommand(
36
                $c->get(Args::class),
37
                $c->get(OutputInterface::class),
38
                $c->get(Migrator::class),
39
                $c->get('migration-path')
40
            );
41
        });
42
43
        $commands[] = $this->registerCommand($container, MigrateInstallCommand::class, function (Container $c) {
44
            return new MigrateInstallCommand(
45
                $c->get(Args::class),
46
                $c->get(OutputInterface::class),
47
                $c->get(DatabaseMigrationRepository::class)
48
            );
49
        });
50
51
        $commands[] = $this->registerCommand($container, MigrateMakeCommand::class, function (Container $c) {
52
            return new MigrateMakeCommand(
53
                $c->get(Args::class),
54
                $c->get(OutputInterface::class),
55
                $c->get(MigrationCreator::class),
56
                $c->get('migration-path')
57
            );
58
        });
59
60 View Code Duplication
        $commands[] = $this->registerCommand($container, MigrateResetCommand::class, function (Container $c) {
61
            return new MigrateResetCommand(
62
                $c->get(Args::class),
63
                $c->get(OutputInterface::class),
64
                $c->get(Migrator::class),
65
                $c->get('migration-path')
66
            );
67
        });
68
69 View Code Duplication
        $commands[] = $this->registerCommand($container, MigrateRollbackCommand::class, function (Container $c) {
70
            return new MigrateRollbackCommand(
71
                $c->get(Args::class),
72
                $c->get(OutputInterface::class),
73
                $c->get(Migrator::class),
74
                $c->get('migration-path')
75
            );
76
        });
77
78 View Code Duplication
        $commands[] = $this->registerCommand($container, MigrateStatusCommand::class, function (Container $c) {
79
            return new MigrateStatusCommand(
80
                $c->get(Args::class),
81
                $c->get(OutputInterface::class),
82
                $c->get(Migrator::class),
83
                $c->get('migration-path')
84
            );
85
        });
86
87
        return $commands;
88
    }
89
90
    public function __invoke(Container $container)
91
    {
92
        $registeredCommands = $this->registerCommands($container);
93
94
        $commands = array_reduce($registeredCommands, function ($acc, $command) {
95
            $acc[$command['name']] = $command['class'];
96
97
            return $acc;
98
        }, []);
99
100
        $container->set('commands', function () use ($commands) {
101
            return $commands;
102
        });
103
104
        $container->set('command', function (Container $c) {
105
            $cmdName = $c->get(Args::class)->getCommand();
106
            $cmdClass = $c->get('commands')[$cmdName];
107
108
            return $c->get($cmdClass);
109
        });
110
    }
111
}
112