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

DatabaseModuleGenerator::setSilentOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
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
            $this->generateFolders();
58
59
            $this->generateModuleJsonFile();
60
61
            if ($this->type !== 'plain') {
62
                $this->generateFiles();
63
                $this->generateResources();
64
            }
65
66
            // Re-check if we created successfully.
67
            $success = $this->module->has($name);
68
            if ($success) {
69
                if (!$this->silentOutput) {
70
                    $this->console->info("Module [{$name}] created successfully.");
71
                }
72
            }
73
74
            return $success;
75
        });
76
    }
77
78
    /**
79
     * Generate the files.
80
     */
81 View Code Duplication
    public function generateFiles()
82
    {
83
        foreach ($this->getFiles() as $stub => $file) {
84
            $path = $this->module->getModulePath($this->getName()) . $file;
85
86
            if (!$this->filesystem->isDirectory($dir = dirname($path))) {
87
                $this->filesystem->makeDirectory($dir, 0775, true);
88
            }
89
90
            $this->filesystem->put($path, $this->getStubContents($stub));
91
92
            if (!$this->silentOutput) {
93
                $this->console->info("Created : {$path}");
94
            }
95
        }
96
    }
97
98
    /**
99
     * Generate some resources.
100
     */
101
    public function generateResources()
102
    {
103
        $moduleName = $this->getName();
104
105
        if (GenerateConfigReader::read('seeder')->generate() === true) {
106
            Artisan::call('module:make-seed', [
107
                'name'     => $moduleName,
108
                'module'   => $moduleName,
109
                '--master' => true,
110
            ]);
111
        }
112
113
        if (GenerateConfigReader::read('provider')->generate() === true) {
114
            Artisan::call('module:make-provider', [
115
                'name'     => $moduleName . 'ServiceProvider',
116
                'module'   => $moduleName,
117
                '--master' => true,
118
            ]);
119
            Artisan::call('module:route-provider', [
120
                'module' => $moduleName,
121
            ]);
122
        }
123
124
        if (GenerateConfigReader::read('controller')->generate() === true) {
125
            $options = $this->type == 'api' ? ['--api' => true] : [];
126
            Artisan::call('module:make-controller', [
127
                    'controller' => $moduleName . 'Controller',
128
                    'module'     => $moduleName,
129
                ] + $options);
130
        }
131
    }
132
133
    /**
134
     * Generate the module.json file
135
     */
136 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...
137
    {
138
        $path = $this->module->getModulePath($this->getName()) . 'module.json';
139
140
        if (!$this->filesystem->isDirectory($dir = dirname($path))) {
141
            $this->filesystem->makeDirectory($dir, 0775, true);
142
        }
143
144
        $this->filesystem->put($path, $this->getStubContents('json'));
145
146
        if (!$this->silentOutput) {
147
            $this->console->info("Created : {$path}");
148
        }
149
    }
150
}
151