Completed
Push — develop ( df9395...08456a )
by Abdelrahman
16:28
created

ModuleMakeCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 176
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 0 24 2
A makeDirectory() 0 8 2
B generateSamples() 0 25 1
B processStubs() 0 29 2
A getNameInput() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Console\Commands;
6
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Str;
9
use Illuminate\Console\Command;
10
11
class ModuleMakeCommand extends Command
12
{
13
    /**
14
     * The filesystem instance.
15
     *
16
     * @var \Illuminate\Filesystem\Filesystem
17
     */
18
    protected $files;
19
20
    /**
21
     * The console command signature.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'make:module {name : The name of the module.}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Create a new module structure';
33
34
    /**
35
     * The type of class being generated.
36
     *
37
     * @var string
38
     */
39
    protected $type = 'Module';
40
41
    /**
42
     * Create a new controller creator command instance.
43
     *
44
     * @param  \Illuminate\Filesystem\Filesystem  $files
45
     *
46
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
47
     */
48
    public function __construct(Filesystem $files)
49
    {
50
        parent::__construct();
51
52
        $this->files = $files;
53
    }
54
55
    /**
56
     * Execute the console command.
57
     *
58
     * @return bool|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
59
     */
60
    public function handle()
61
    {
62
        $name = $this->getNameInput();
63
64
        $path = app_path($name);
65
66
        // First we will check to see if the class already exists. If it does, we don't want
67
        // to create the class and overwrite the user's code. So, we will bail out so the
68
        // code is untouched. Otherwise, we will continue generating this class' files.
69
        if ($this->files->exists($path)) {
70
            $this->error($this->type.' already exists!');
71
72
            return false;
73
        }
74
75
        // Next, we will generate the path to the location where this class' file should get
76
        // written. Then, we will build the class and make the proper replacements on the
77
        // stub files so that it gets the correctly formatted namespace and class name.
78
        $stubs = __DIR__.'/../../../resources/stubs/module';
79
        $this->processStubs($stubs, $path);
80
        $this->generateSamples();
81
82
        $this->info($this->type.' created successfully.');
83
    }
84
85
    /**
86
     * Build the directory for the class if necessary.
87
     *
88
     * @param  string  $path
89
     * @return string
90
     */
91
    protected function makeDirectory($path)
92
    {
93
        if (! $this->files->isDirectory($path)) {
94
            $this->files->makeDirectory($path, 0777, true, true);
95
        }
96
97
        return $path;
98
    }
99
100
    /**
101
     * Generate code samples.
102
     *
103
     * @return void
104
     */
105
    protected function generateSamples()
106
    {
107
        $this->call('make:config', ['name' => 'config', '--module' => $this->getNameInput()]);
108
        $this->call('make:model', ['name' => 'Test', '--module' => $this->getNameInput()]);
109
        $this->call('make:policy', ['name' => 'TestPolicy', '--module' => $this->getNameInput()]);
110
        $this->call('make:provider', ['name' => ucfirst(str_after($this->getNameInput(), '/')).'ServiceProvider', '--module' => $this->getNameInput()]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 152 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
111
        $this->call('make:command', ['name' => 'TestCommand', '--module' => $this->getNameInput()]);
112
        $this->call('make:controller', ['name' => 'TestController', '--module' => $this->getNameInput()]);
113
        $this->call('make:request', ['name' => 'TestRequest', '--module' => $this->getNameInput()]);
114
        $this->call('make:middleware', ['name' => 'TestMiddleware', '--module' => $this->getNameInput()]);
115
        $this->call('make:transformer', ['name' => 'TestTransformer', '--model' => 'Test', '--module' => $this->getNameInput()]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
116
        $this->call('make:datatable', ['name' => 'TestDatatable', '--model' => 'Test', '--transformer' => 'TestTransformer', '--module' => $this->getNameInput()]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
117
        //$this->call('make:config', ['name' => 'config', '--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
        //$this->call('make:config', ['name' => 'config', '--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
        //$this->call('make:config', ['name' => 'config', '--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
        //$this->call('make:config', ['--module' => $this->getNameInput()]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
    }
130
131
    /**
132
     * Process stubs placeholders.
133
     *
134
     * @param  string  $stubs
135
     * @param  string  $path
136
     *
137
     * @return void
138
     */
139
    protected function processStubs($stubs, $path)
140
    {
141
        $this->makeDirectory($path);
142
        $this->files->copyDirectory($stubs, $path);
143
144
        $files = [
145
            ($phpunit = $path.DIRECTORY_SEPARATOR.'phpunit.xml.dist') => $this->files->get($phpunit),
146
            ($composer = $path.DIRECTORY_SEPARATOR.'composer.json') => $this->files->get($composer),
147
            ($changelog = $path.DIRECTORY_SEPARATOR.'CHANGELOG.md') => $this->files->get($changelog),
148
            ($readme = $path.DIRECTORY_SEPARATOR.'README.md') => $this->files->get($readme),
149
        ];
150
151
        $module = ucfirst(str_after($this->getNameInput(), '/'));
152
        $name = implode(' ', array_map('ucfirst', explode('/', $this->getNameInput())));
153
        $jsonNamespace = implode('\\\\', array_map('ucfirst', explode('/', $this->getNameInput())));
154
155
        foreach ($files as $key => &$file) {
156
            $file = str_replace('DummyModuleName', $name, $file);
157
            $file = str_replace('Dummy\\Module', $jsonNamespace, $file);
158
            $file = str_replace('DummyModuleServiceProvider', $jsonNamespace."\\\\Providers\\\\{$module}ServiceProvider", $file);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
159
160
            $file = str_replace('dummy/module', $this->getNameInput(), $file);
161
            $file = str_replace('dummy-module', str_replace('/', '-', $this->getNameInput()), $file);
162
            $file = str_replace('dummy:module', str_replace('/', ':', $this->getNameInput()), $file);
163
            $file = str_replace('dummy.module', str_replace('/', '.', $this->getNameInput()), $file);
164
165
            $this->files->put($key, $file);
166
        }
167
    }
168
169
    /**
170
     * Get the desired class name from the input.
171
     *
172
     * @throws \Exception
173
     *
174
     * @return string
175
     */
176
    protected function getNameInput()
177
    {
178
        $name = trim($this->argument('name'));
179
180
        if (strpos($name, '/') === false) {
181
            throw new \Exception('Module name must consist of two segments: vendor/module');
182
        }
183
184
        return $name;
185
    }
186
}
187