Completed
Pull Request — master (#1163)
by
unknown
01:47
created

DatabaseModuleGenerator::generate()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 49

Duplication

Lines 12
Ratio 24.49 %

Importance

Changes 0
Metric Value
cc 8
nc 1
nop 0
dl 12
loc 49
rs 7.8682
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Generators;
4
5
use Illuminate\Support\Facades\Artisan;
6
use Illuminate\Support\Facades\DB;
7
use Nwidart\Modules\Laravel\LaravelDatabaseRepository;
8
use Nwidart\Modules\Support\Config\GenerateConfigReader;
9
10
/**
11
 * Class DatabaseModuleGenerator.
12
 * @package Nwidart\Modules\Generators
13
 * @property LaravelDatabaseRepository $module
14
 */
15
class DatabaseModuleGenerator extends ModuleGenerator
16
{
17
    protected $silentOutput = false;
18
19
    public function setSilentOutput($bool = true)
20
    {
21
        $this->silentOutput = $bool;
22
23
        return $this;
24
    }
25
26
    /**
27
     * Generate the module.
28
     */
29
    public function generate(): int
30
    {
31
        return DB::transaction(function () {
32
            $name = $this->getName();
33
34 View Code Duplication
            if ($this->module->has($name)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
35
                if ($this->force) {
36
                    $this->module->delete($name);
37
                } else {
38
                    if (!$this->silentOutput) {
39
                        $this->console->info("Module [{$name}] already exist!");
40
                    } else {
41
                        abort(400, "Module [{$name}] already exist!");
42
                    }
43
44
                    return false;
45
                }
46
            }
47
48
            // Get data from module.json.
49
            $data = $this->getStubContents('json');
50
            $data = json_decode($data, true);
51
            $data['path'] = $this->module->getModulePath($this->getName());
52
            if ($this->type === 'plain') {
53
                $data['provider'] = [];
54
            }
55
            $this->module->getModel()->create($data);
56
57
            $success = false;
58
            // Re-check if we created successfully.
59
            if ($this->module->has($name)) {
60
                $this->generateFolders();
61
62
                $this->generateModuleJsonFile();
63
64
                if ($this->type !== 'plain') {
65
                    $this->generateFiles();
66
                    $this->generateResources();
67
                }
68
69
                $success = true;
70
                if (!$this->silentOutput) {
71
                    $this->console->info("Module [{$name}] created successfully.");
72
                }
73
            }
74
75
            return $success;
76
        });
77
    }
78
79
    /**
80
     * Generate the files.
81
     */
82 View Code Duplication
    public function generateFiles()
83
    {
84
        foreach ($this->getFiles() as $stub => $file) {
85
            $path = $this->module->getModulePath($this->getName()) . $file;
86
87
            if (!$this->filesystem->isDirectory($dir = dirname($path))) {
88
                $this->filesystem->makeDirectory($dir, 0775, true);
89
            }
90
91
            $this->filesystem->put($path, $this->getStubContents($stub));
92
93
            if (!$this->silentOutput) {
94
                $this->console->info("Created : {$path}");
95
            }
96
        }
97
    }
98
99
    /**
100
     * Generate some resources.
101
     */
102
    public function generateResources()
103
    {
104
        $moduleName = $this->getName();
105
106
        if (GenerateConfigReader::read('seeder')->generate() === true) {
107
            Artisan::call('module:make-seed', [
108
                'name'     => $moduleName,
109
                'module'   => $moduleName,
110
                '--master' => true,
111
            ]);
112
        }
113
114
        if (GenerateConfigReader::read('provider')->generate() === true) {
115
            Artisan::call('module:make-provider', [
116
                'name'     => $moduleName . 'ServiceProvider',
117
                'module'   => $moduleName,
118
                '--master' => true,
119
            ]);
120
            Artisan::call('module:route-provider', [
121
                'module' => $moduleName,
122
            ]);
123
        }
124
125
        if (GenerateConfigReader::read('controller')->generate() === true) {
126
            $options = $this->type == 'api' ? ['--api' => true] : [];
127
            Artisan::call('module:make-controller', [
128
                    'controller' => $moduleName . 'Controller',
129
                    'module'     => $moduleName,
130
                ] + $options);
131
        }
132
    }
133
134
    /**
135
     * Generate the module.json file
136
     */
137 View Code Duplication
    private function generateModuleJsonFile()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
138
    {
139
        $path = $this->module->getModulePath($this->getName()) . 'module.json';
140
141
        if (!$this->filesystem->isDirectory($dir = dirname($path))) {
142
            $this->filesystem->makeDirectory($dir, 0775, true);
143
        }
144
145
        $this->filesystem->put($path, $this->getStubContents('json'));
146
147
        if (!$this->silentOutput) {
148
            $this->console->info("Created : {$path}");
149
        }
150
    }
151
}
152