Completed
Pull Request — master (#322)
by Mathieu
03:43
created

PublishConfigurationCommand::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class PublishConfigurationCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'module:publish-config';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Publish a module\'s config files to the application';
24
25
    /**
26
     * Execute the console command.
27
     */
28 View Code Duplication
    public function handle()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        if ($module = $this->argument('module')) {
31
            $this->publishConfiguration($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by $this->argument('module') on line 30 can also be of type array; however, Nwidart\Modules\Commands...:publishConfiguration() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
33
            return;
34
        }
35
36
        foreach ($this->laravel['modules']->enabled() as $module) {
37
            $this->publishConfiguration($module->getName());
38
        }
39
    }
40
41
    /**
42
     * @param string $module
43
     * @return string
44
     */
45
    private function getServiceProviderForModule($module)
46
    {
47
        $namespace = $this->laravel['config']->get('modules.namespace');
48
        $studlyName = studly_case($module);
49
50
        return "$namespace\\$studlyName\\Providers\\{$studlyName}ServiceProvider";
51
    }
52
53
    /**
54
     * @param string $module
55
     */
56
    private function publishConfiguration($module)
57
    {
58
        $this->call('vendor:publish', [
59
            '--provider' => $this->getServiceProviderForModule($module),
60
            '--force' => $this->option('force'),
61
            '--tag' => ['config'],
62
        ]);
63
    }
64
65
    /**
66
     * Get the console command arguments.
67
     *
68
     * @return array
69
     */
70 56
    protected function getArguments()
71
    {
72
        return [
73 56
            ['module', InputArgument::OPTIONAL, 'The name of module being used.'],
74
        ];
75
    }
76
77
    /**
78
     * @return array
79
     */
80 56
    protected function getOptions()
81
    {
82
        return [
83 56
            ['--force', '-f', InputOption::VALUE_NONE, 'Force the publishing of config files'],
84
        ];
85
    }
86
}
87