Completed
Pull Request — master (#1163)
by
unknown
02:19
created

DatabaseModuleGenerator::generate()   B

Complexity

Conditions 8
Paths 22

Size

Total Lines 48

Duplication

Lines 12
Ratio 25 %

Importance

Changes 0
Metric Value
cc 8
nc 22
nop 0
dl 12
loc 48
rs 7.8901
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
     * @todo Need to generate module.json ?
28
     */
29
    public function generate(): int
30
    {
31
        $name = $this->getName();
32
33 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...
34
            /**
35
             * Re-use from parent method.
36
             */
37
            if ($this->force) {
38
                $this->module->delete($name);
39
            } else {
40
                if (!$this->silentOutput) {
41
                    $this->console->info("Module [{$name}] already exist!");
42
                } else {
43
                    abort(400, "Module [{$name}] already exist!");
44
                }
45
46
                return false;
47
            }
48
        }
49
50
        // Get data from module.json.
51
        $data = $this->getStubContents('json');
52
        $data = json_decode($data, true);
53
        $data['path'] = $this->module->getModulePath($this->getName());
54
        if ($this->type === 'plain') {
55
            $data['provider'] = [];
56
        }
57
        $this->module->getModel()->create($data);
58
59
        $success = false;
60
        // Re-check if we created successfully.
61
        if ($this->module->has($name)) {
62
            $this->generateFolders();
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
     * 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