Completed
Pull Request — master (#1163)
by
unknown
02:32
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\Support\Config\GenerateConfigReader;
7
8
class DatabaseModuleGenerator extends ModuleGenerator
9
{
10
    protected $silentOutput = false;
11
12
    public function setSilentOutput($bool = true)
13
    {
14
        $this->silentOutput = $bool;
15
16
        return $this;
17
    }
18
19
    /**
20
     * Generate the module.
21
     */
22
    public function generate(): int
23
    {
24
        $name = $this->getName();
25
26 View Code Duplication
        if ($this->module->has($name)) {
0 ignored issues
show
Documentation Bug introduced by
The method has does not exist on object<Nwidart\Modules\Module>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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...
27
            if ($this->force) {
28
                $this->module->delete($name);
0 ignored issues
show
Unused Code introduced by
The call to Module::delete() has too many arguments starting with $name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
29
            } else {
30
                if (!$this->silentOutput) {
31
                    $this->console->info("Module [{$name}] already exist!");
32
                    return false;
33
                } else {
34
                    abort(400, "Module [{$name}] already exist!");
35
                }
36
            }
37
        }
38
39
        // Get data from module.json.
40
        $data = $this->getStubContents('json');
41
        $data = json_decode($data, true);
42
        $data['path'] = $this->module->getModulePath($this->getName());
0 ignored issues
show
Documentation Bug introduced by
The method getModulePath does not exist on object<Nwidart\Modules\Module>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
        if ($this->type ===  'plain') {
44
            $data['provider'] = [];
45
        }
46
        $this->module->getModel()->create($data);
0 ignored issues
show
Documentation Bug introduced by
The method getModel does not exist on object<Nwidart\Modules\Module>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
48
        $success = false;
49
        // Re-check if we created successfully.
50
        if ($this->module->has($name)) {
0 ignored issues
show
Documentation Bug introduced by
The method has does not exist on object<Nwidart\Modules\Module>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
            $this->generateFolders();
52
53
            if ($this->type !== 'plain') {
54
                $this->generateFiles();
55
                $this->generateResources();
56
            }
57
58
            $success = true;
59
            if (!$this->silentOutput) {
60
                $this->console->info("Module [{$name}] created successfully.");
61
            }
62
        }
63
64
        return $success;
65
    }
66
67
    /**
68
     * Generate the files.
69
     */
70 View Code Duplication
    public function generateFiles()
71
    {
72
        foreach ($this->getFiles() as $stub => $file) {
73
            $path = $this->module->getModulePath($this->getName()) . $file;
0 ignored issues
show
Documentation Bug introduced by
The method getModulePath does not exist on object<Nwidart\Modules\Module>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
75
            if (!$this->filesystem->isDirectory($dir = dirname($path))) {
76
                $this->filesystem->makeDirectory($dir, 0775, true);
77
            }
78
79
            $this->filesystem->put($path, $this->getStubContents($stub));
80
81
            if (!$this->silentOutput) {
82
                $this->console->info("Created : {$path}");
83
            }
84
        }
85
    }
86
87
    /**
88
     * Generate some resources.
89
     */
90
    public function generateResources()
91
    {
92 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...
93
            Artisan::call('module:make-seed', [
94
                'name'     => $this->getName(),
95
                'module'   => $this->getName(),
96
                '--master' => true,
97
            ]);
98
        }
99
100
        if (GenerateConfigReader::read('provider')->generate() === true) {
101
            Artisan::call('module:make-provider', [
102
                'name'     => $this->getName() . 'ServiceProvider',
103
                'module'   => $this->getName(),
104
                '--master' => true,
105
            ]);
106
            Artisan::call('module:route-provider', [
107
                'module' => $this->getName(),
108
            ]);
109
        }
110
111 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...
112
            $options = $this->type == 'api' ? ['--api' => true] : [];
113
            Artisan::call('module:make-controller', [
114
                    'controller' => $this->getName() . 'Controller',
115
                    'module'     => $this->getName(),
116
                ] + $options);
117
        }
118
    }
119
}
120