Completed
Pull Request — master (#1163)
by
unknown
01:36
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
                    return false;
39
                } else {
40
                    abort(400, "Module [{$name}] already exist!");
41
                }
42
            }
43
        }
44
45
        // Get data from module.json.
46
        $data = $this->getStubContents('json');
47
        $data = json_decode($data, true);
48
        $data['path'] = $this->module->getModulePath($this->getName());
49
        if ($this->type === 'plain') {
50
            $data['provider'] = [];
51
        }
52
        $this->module->getModel()->create($data);
53
54
        $success = false;
55
        // Re-check if we created successfully.
56
        if ($this->module->has($name)) {
57
            $this->generateFolders();
58
59
            if ($this->type !== 'plain') {
60
                $this->generateFiles();
61
                $this->generateResources();
62
            }
63
64
            $success = true;
65
            if (!$this->silentOutput) {
66
                $this->console->info("Module [{$name}] created successfully.");
67
            }
68
        }
69
70
        return $success;
71
    }
72
73
    /**
74
     * Generate the files.
75
     */
76 View Code Duplication
    public function generateFiles()
77
    {
78
        foreach ($this->getFiles() as $stub => $file) {
79
            $path = $this->module->getModulePath($this->getName()) . $file;
80
81
            if (!$this->filesystem->isDirectory($dir = dirname($path))) {
82
                $this->filesystem->makeDirectory($dir, 0775, true);
83
            }
84
85
            $this->filesystem->put($path, $this->getStubContents($stub));
86
87
            if (!$this->silentOutput) {
88
                $this->console->info("Created : {$path}");
89
            }
90
        }
91
    }
92
93
    /**
94
     * Generate some resources.
95
     */
96
    public function generateResources()
97
    {
98 View Code Duplication
        if (GenerateConfigReader::read('seeder')->generate() === true) {
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...
99
            Artisan::call('module:make-seed', [
100
                'name'     => $this->getName(),
101
                'module'   => $this->getName(),
102
                '--master' => true,
103
            ]);
104
        }
105
106
        if (GenerateConfigReader::read('provider')->generate() === true) {
107
            Artisan::call('module:make-provider', [
108
                'name'     => $this->getName() . 'ServiceProvider',
109
                'module'   => $this->getName(),
110
                '--master' => true,
111
            ]);
112
            Artisan::call('module:route-provider', [
113
                'module' => $this->getName(),
114
            ]);
115
        }
116
117 View Code Duplication
        if (GenerateConfigReader::read('controller')->generate() === true) {
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...
118
            $options = $this->type == 'api' ? ['--api' => true] : [];
119
            Artisan::call('module:make-controller', [
120
                    'controller' => $this->getName() . 'Controller',
121
                    'module'     => $this->getName(),
122
                ] + $options);
123
        }
124
    }
125
}
126