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

DatabaseModuleGenerator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 24.14 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 7
dl 28
loc 116
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setSilentOutput() 0 6 1
B generate() 12 45 8
A generateFiles() 16 16 4
A generateResources() 0 31 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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