Completed
Pull Request — master (#1163)
by
unknown
09:19
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 Nwidart\Modules\Laravel\LaravelDatabaseRepository;
7
use Nwidart\Modules\Support\Config\GenerateConfigReader;
8
9
/**
10
 * Class DatabaseModuleGenerator.
11
 * @package Nwidart\Modules\Generators
12
 * @property LaravelDatabaseRepository $module
13
 */
14
class DatabaseModuleGenerator extends ModuleGenerator
15
{
16
    protected $silentOutput = false;
17
18
    public function setSilentOutput($bool = true)
19
    {
20
        $this->silentOutput = $bool;
21
22
        return $this;
23
    }
24
25
    /**
26
     * Generate the module.
27
     */
28
    public function generate(): int
29
    {
30
        $name = $this->getName();
31
32 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...
33
            if ($this->force) {
34
                $this->module->delete($name);
35
            } else {
36
                if (!$this->silentOutput) {
37
                    $this->console->info("Module [{$name}] already exist!");
38
                } else {
39
                    abort(400, "Module [{$name}] already exist!");
40
                }
41
42
                return false;
43
            }
44
        }
45
46
        // Get data from module.json.
47
        $data = $this->getStubContents('json');
48
        $data = json_decode($data, true);
49
        $data['path'] = $this->module->getModulePath($this->getName());
50
        if ($this->type === 'plain') {
51
            $data['provider'] = [];
52
        }
53
        $this->module->getModel()->create($data);
54
55
        $success = false;
56
        // Re-check if we created successfully.
57
        if ($this->module->has($name)) {
58
            $this->generateFolders();
59
60
            if ($this->type !== 'plain') {
61
                $this->generateFiles();
62
                $this->generateResources();
63
            }
64
65
            $success = true;
66
            if (!$this->silentOutput) {
67
                $this->console->info("Module [{$name}] created successfully.");
68
            }
69
        }
70
71
        return $success;
72
    }
73
74
    /**
75
     * Generate the files.
76
     */
77 View Code Duplication
    public function generateFiles()
78
    {
79
        foreach ($this->getFiles() as $stub => $file) {
80
            $path = $this->module->getModulePath($this->getName()) . $file;
81
82
            if (!$this->filesystem->isDirectory($dir = dirname($path))) {
83
                $this->filesystem->makeDirectory($dir, 0775, true);
84
            }
85
86
            $this->filesystem->put($path, $this->getStubContents($stub));
87
88
            if (!$this->silentOutput) {
89
                $this->console->info("Created : {$path}");
90
            }
91
        }
92
    }
93
94
    /**
95
     * Generate some resources.
96
     */
97
    public function generateResources()
98
    {
99
        $moduleName = $this->getName();
100
101
        if (GenerateConfigReader::read('seeder')->generate() === true) {
102
            Artisan::call('module:make-seed', [
103
                'name'     => $moduleName,
104
                'module'   => $moduleName,
105
                '--master' => true,
106
            ]);
107
        }
108
109
        if (GenerateConfigReader::read('provider')->generate() === true) {
110
            Artisan::call('module:make-provider', [
111
                'name'     => $moduleName . 'ServiceProvider',
112
                'module'   => $moduleName,
113
                '--master' => true,
114
            ]);
115
            Artisan::call('module:route-provider', [
116
                'module' => $moduleName,
117
            ]);
118
        }
119
120
        if (GenerateConfigReader::read('controller')->generate() === true) {
121
            $options = $this->type == 'api' ? ['--api' => true] : [];
122
            Artisan::call('module:make-controller', [
123
                    'controller' => $moduleName . 'Controller',
124
                    'module'     => $moduleName,
125
                ] + $options);
126
        }
127
    }
128
}
129