GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 560752...2363e1 )
by Shea
02:46
created

MakeModuleCommand::generate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9714
cc 2
eloc 15
nc 2
nop 0
1
<?php
2
namespace Caffeinated\Modules\Console\Generators;
3
4
use Caffeinated\Modules\Modules;
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
class MakeModuleCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'make:module';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new Caffeinated module and bootstrap it';
26
27
    /**
28
     * Module folders to be created.
29
     *
30
	 * @var array
31
	 */
32
	protected $moduleFolders = [
33
		'Console/',
34
		'Database/',
35
		'Database/Migrations/',
36
		'Database/Seeds/',
37
		'Http/',
38
		'Http/Controllers/',
39
		'Http/Middleware/',
40
		'Http/Requests/',
41
		'Providers/',
42
		'Resources/',
43
		'Resources/Lang/',
44
		'Resources/Views/',
45
	];
46
47
	/**
48
     * Module files to be created.
49
     *
50
	 * @var array
51
	 */
52
	protected $moduleFiles = [
53
		'Database/Seeds/{{namespace}}DatabaseSeeder.php',
54
		'Http/routes.php',
55
		'Providers/{{namespace}}ServiceProvider.php',
56
		'Providers/RouteServiceProvider.php',
57
		'module.json'
58
	];
59
60
	/**
61
     * Module stubs used to populate defined files.
62
     *
63
	 * @var array
64
	 */
65
	protected $moduleStubs = [
66
		'seeder.stub',
67
		'routes.stub',
68
		'moduleserviceprovider.stub',
69
		'routeserviceprovider.stub',
70
		'manifest.stub'
71
	];
72
73
	/**
74
     * The modules instance.
75
     *
76
	 * @var Modules
77
	 */
78
	protected $module;
79
80
	/**
81
     * The filesystem instance.
82
     *
83
	 * @var Filesystem
84
	 */
85
	protected $files;
86
87
    /**
88
     * Array to store the configuration details.
89
     *
90
     * @var array
91
     */
92
    protected $container;
93
94
    /**
95
     * Create a new command instance.
96
     *
97
     * @param Filesystem  $files
98
     * @param Modules  $module
99
     */
100
    public function __construct(Filesystem $files, Modules $module)
101
    {
102
        parent::__construct();
103
104
        $this->files  = $files;
105
        $this->module = $module;
106
    }
107
108
    /**
109
     * Execute the console command.
110
     *
111
     * @return mixed
112
     */
113
    public function fire()
114
    {
115
        $this->container['slug']      = strtolower($this->argument('slug'));
116
        $this->container['name']      = Str::studly($this->container['slug']);
117
        $this->container['namespace'] = Str::studly($this->container['slug']);
118
119
        $this->displayHeader('make_module_introduction');
120
121
        $this->stepOne();
122
    }
123
124
    /**
125
     * Step 1: Configure module manifest.
126
     *
127
     * @return mixed
128
     */
129
    private function stepOne()
130
    {
131
        $this->displayHeader('make_module_step_1');
132
133
        $this->container['name']        = $this->ask('Please enter the name of the module:', $this->container['name']);
134
        $this->container['slug']        = $this->ask('Please enter the slug for the module:', $this->container['slug']);
135
        $this->container['namespace']   = $this->ask('Please enter the namespace for the module:', $this->container['namespace']);
136
        $this->container['version']     = $this->ask('Please enter the module version:', '1.0');
137
        $this->container['description'] = $this->ask('Please enter the description of the module:', 'This is the description for the '.$this->container['name'].' module.');
138
        $this->container['author']      = $this->ask('Please enter the author of the module:', ' ');
139
        $this->container['license']     = $this->ask('Please enter the module license:', 'MIT');
140
141
        $this->comment('You have provided the following manifest information:');
142
        $this->comment('Name:        '.$this->container['name']);
143
        $this->comment('Slug:        '.$this->container['slug']);
144
        $this->comment('Namespace:   '.$this->container['namespace']);
145
        $this->comment('Version:     '.$this->container['version']);
146
        $this->comment('Description: '.$this->container['description']);
147
        $this->comment('Author:      '.$this->container['author']);
148
        $this->comment('License:     '.$this->container['license']);
149
150
        if ($this->confirm('Do you wish to continue?')) {
151
            $this->comment('Thanks! That\'s all we need.');
152
			$this->comment('Now relax while your module is generated for you.');
153
154
            $this->generate();
155
        } else {
156
            return $this->stepOne();
157
        }
158
159
        return true;
160
    }
161
162
    /**
163
     * Generate the module.
164
     */
165
    protected function generate()
166
    {
167
        $steps = [
168
            'Generating folders...'     => 'generateFolders',
169
            'Generating .gitkeep...'    => 'generateGitkeep',
170
            'Generating files...'       => 'generateFiles',
171
            'Resetting module cache...' => 'resetCache',
172
            'Optimizing Laravel...'     => 'optimizeLaravel'
173
        ];
174
175
        $progress = new ProgressBar($this->output, count($steps));
176
        $progress->start();
177
178
        foreach ($steps as $message => $function) {
179
            $progress->setMessage($message);
180
181
            $this->$function();
182
183
            $progress->advance();
184
        }
185
186
        $progress->finish();
187
        $this->info("\nModule generated successfully.");
188
    }
189
190
    /**
191
	 * Generate defined module folders.
192
	 *
193
	 * @return void
194
	 */
195
	protected function generateFolders()
196
	{
197
		if (! $this->files->isDirectory($this->module->getPath())) {
198
			$this->files->makeDirectory($this->module->getPath());
199
		}
200
201
		$this->files->makeDirectory($this->getModulePath($this->container['slug'], true));
202
203
		foreach ($this->moduleFolders as $folder) {
204
			$this->files->makeDirectory($this->getModulePath($this->container['slug']).$folder);
205
		}
206
	}
207
208
    /**
209
	 * Generate defined module files.
210
	 *
211
	 * @return void
212
	 */
213
	protected function generateFiles()
214
	{
215
		foreach ($this->moduleFiles as $key => $file) {
216
			$file = $this->formatContent($file);
217
218
			$this->files->put($this->getDestinationFile($file), $this->getStubContent($key));
219
		}
220
	}
221
222
    /**
223
     * Generate .gitkeep files within generated folders.
224
     *
225
     * @return null
226
     */
227
    protected function generateGitkeep()
228
    {
229
        $modulePath = $this->getModulePath($this->container['slug']);
230
        foreach ($this->moduleFolders as $folder) {
231
            $gitkeep = $modulePath.$folder.'/.gitkeep';
232
            $this->files->put($gitkeep, '');
233
        }
234
    }
235
236
    /**
237
     * Reset module cache of enabled and disabled modules.
238
     *
239
     * @return void
240
     */
241
    protected function resetCache()
242
    {
243
        return $this->callSilent('module:cache');
244
    }
245
246
    /**
247
     * Optimize Laravel for better performance.
248
     *
249
     * @return void
250
     */
251
    protected function optimizeLaravel()
252
    {
253
        return $this->callSilent('optimize');
254
    }
255
256
    /**
257
	 * Get the path to the module.
258
	 *
259
	 * @param  string $slug
260
	 * @return string
261
	 */
262
	protected function getModulePath($slug = null, $allowNotExists = false)
263
	{
264
		if ($slug) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
265
            return $this->module->getModulePath($slug, $allowNotExists);
0 ignored issues
show
Unused Code introduced by
The call to Modules::getModulePath() has too many arguments starting with $allowNotExists.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
266
        }
267
268
		return $this->module->getPath();
269
	}
270
271
	/**
272
	 * Get destination file.
273
	 *
274
	 * @param  string $file
275
	 * @return string
276
	 */
277
	protected function getDestinationFile($file)
278
	{
279
		return $this->getModulePath($this->container['slug']).$this->formatContent($file);
280
	}
281
282
	/**
283
	 * Get stub content by key.
284
	 *
285
	 * @param int  $key
286
	 * @return string
287
	 */
288
	protected function getStubContent($key)
289
	{
290
		return $this->formatContent($this->files->get(__DIR__.'/../../../resources/stubs/'.$this->moduleStubs[$key]));
291
	}
292
293
	/**
294
	 * Replace placeholder text with correct values.
295
	 *
296
	 * @return string
297
	 */
298
	protected function formatContent($content)
299
	{
300
		return str_replace(
301
			['{{slug}}', '{{name}}', '{{namespace}}', '{{version}}', '{{description}}', '{{author}}', '{{path}}'],
302
			[$this->container['slug'], $this->container['name'], $this->container['namespace'], $this->container['version'], $this->container['description'], $this->container['author'], $this->module->getNamespace()],
303
			$content
304
		);
305
	}
306
307
	/**
308
	 * Pull the given stub file contents and display them on screen.
309
	 *
310
	 * @param string  $file
311
	 * @param string  $level
312
	 * @return mixed
313
	 */
314
	protected function displayHeader($file = '', $level = 'info')
315
	{
316
		$stub = $this->files->get(__DIR__.'/../../../resources/stubs/console/'.$file.'.stub');
317
		return $this->$level($stub);
318
	}
319
320
    /**
321
     * Get the console command arguments.
322
     *
323
     * @return array
324
     */
325
    protected function getArguments()
326
    {
327
        return [
328
            ['slug', InputArgument::REQUIRED, 'The slug of the module']
329
        ];
330
    }
331
}
332