ModuleMake   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 117
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createModule() 0 11 1
B proceed() 0 24 3
A createModuleObject() 0 4 1
B addModuleToConfigurationFile() 0 33 3
1
<?php
2
3
namespace Mnabialek\LaravelModular\Console\Commands;
4
5
use Mnabialek\LaravelModular\Models\Module;
6
use Mnabialek\LaravelModular\Traits\Replacer;
7
use Mnabialek\LaravelModular\Console\Traits\ModuleCreator;
8
9
class ModuleMake extends BaseCommand
10
{
11
    use Replacer;
12
    use ModuleCreator;
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'module:make 
19
    {module* : Module name (or multiple module names space separated)} 
20
    {--group= : Stub group name that will be used for creating this module}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Generates new module structure.';
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function proceed()
33
    {
34
        $moduleNames = collect($this->argument('module'))->unique();
35
36
        $stubGroup = $this->getStubGroup();
37
38
        // verify whether stub directory exists
39
        $this->verifyStubGroup($stubGroup);
40
41
        $moduleNames->each(function ($moduleName) use ($stubGroup) {
42
            $module = $this->createModuleObject($moduleName);
43
44
            // module added to configuration or module directory exists
45
            if ($this->laravel['modular']->exists($module->name())
46
                || $this->exists($module->directory())
47
            ) {
48
                $this->warn("[Module {$module->name()}] Module already exists - ignoring");
49
            } else {
50
                // module does not exist - let's create it
51
                $this->createModule($module, $stubGroup);
52
                $this->info("[Module {$module->name()}] Module was generated");
53
            }
54
        });
55
    }
56
57
    /**
58
     * Create module object (it does not mean module exists)
59
     *
60
     * @param string $moduleName
61
     *
62
     * @return Module
63
     */
64
    protected function createModuleObject($moduleName)
65
    {
66
        return new Module($moduleName, $this->laravel);
67
    }
68
69
    /**
70
     * Create module
71
     *
72
     * @param Module $module
73
     * @param string $stubGroup
74
     */
75
    protected function createModule(Module $module, $stubGroup)
76
    {
77
        // first create directories
78
        $this->createModuleDirectories($module, $stubGroup);
79
80
        // now create files
81
        $this->createModuleFiles($module, $stubGroup);
82
83
        // finally add module to configuration (if not disabled in config)
84
        $this->addModuleToConfigurationFile($module);
85
    }
86
87
    /**
88
     * Add module to configuration file
89
     *
90
     * @param $module
91
     */
92
    protected function addModuleToConfigurationFile(Module $module)
93
    {
94
        $configFile = $this->laravel['modular.config']->configPath();
95
96
        if (!$this->laravel['modular.config']->autoAdd()) {
97
            $this->info("[Module {$module->name()}] - auto-adding to config file turned off\n" .
98
                "Please add this module manually into {$configFile} file if you want to use it");
99
100
            return;
101
        }
102
103
        // getting modified content of config file
104
        $result =
105
            preg_replace_callback($this->laravel['modular.config']->autoAddPattern(),
106
                function ($matches) use ($module, $configFile) {
107
                    return $matches[1] . $matches[2] .
108
                    $this->replace($this->laravel['modular.config']->autoAddTemplate(),
109
                        $module) .
110
                    $matches[3];
111
                },
112
                $this->laravel['files']->get($configFile), -1, $count);
113
114
        if ($count) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
115
            // found place where new module should be added into config file
116
            $this->laravel['files']->put($configFile, $result);
117
            $this->comment("[Module {$module->name()}] Added into config file {$configFile}");
118
        } else {
119
            // cannot add module to config file automatically
120
            $this->warn("[Module {$module->name()}] It was impossible to add module into {$configFile}" .
121
                " file.\n Please make sure you haven't changed structure of this file. " .
122
                "At the moment add <info>{$module->name()}</info> to this file manually");
123
        }
124
    }
125
}
126