Passed
Pull Request — master (#138)
by
unknown
07:14
created

LaraStructure::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
8
class LaraStructure extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'make:structure {model} {--m|migration}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'This command generate model and a base repository out of the box.';
23
24
    /**
25
     * Filesystem instance
26
     * 
27
     * @var string
28
     */
29
    protected $filesystem;
30
31
    /**
32
     * Default laracom folder
33
     * 
34
     * @var string
35
     */
36
    protected $folder;
37
38
    /**
39
     * Create a new command instance.
40
     *
41
     * @return void
42
     */
43
    public function __construct(Filesystem $filesystem)
44
    {
45
        parent::__construct();
46
        $this->filesystem = $filesystem;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filesystem of type Illuminate\Filesystem\Filesystem is incompatible with the declared type string of property $filesystem.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return mixed
53
     */
54
    public function handle()
55
    {
56
        // Get model name from developer
57
        $this->model = ucfirst($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') can also be of type string[]; however, parameter $str of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $this->model = ucfirst(/** @scrutinizer ignore-type */ $this->argument('model'));
Loading history...
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
59
        // Get plural name for the given model
60
        $pluralModel = str_plural($this->model);
61
62
        // Check if the model already created
63
        if ( $this->filesystem->exists(app_path("Shop/{$pluralModel}/{$this->model}.php")) ){
64
            return $this->error("The given model already exists!");
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->error('The given model already exists!') targeting Illuminate\Console\Command::error() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
        }
66
67
        // create all structured folders
68
        $this->createFolders('Shop');
69
70
        $this->createFile(
71
            app_path('Console/Stubs/DummyRepository.stub'),
72
            app_path("Shop/{$pluralModel}/Repositories/{$this->model}Repository.php")
73
        );
74
75
        $this->createFile(
76
            app_path('Console/Stubs/DummyRepositoryInterface.stub'),
77
            app_path("Shop/{$pluralModel}/Repositories/Interfaces/{$this->model}RepositoryInterface.php")
78
        );
79
80
        $this->info('File structure for ' . $this->model . ' created.');
81
        
82
        // Create Model under default instalation folder
83
        $this->call('make:model', [
84
            'name' => 'Shop/' . $pluralModel . '/' .$this->model,
85
            '--migration' => $this->option('migration'),
86
        ]);
87
    }
88
89
    /**
90
     * Create source from dummy model name
91
     * 
92
     * @param  string $dummy        
93
     * @param  string $destinationPath
94
     * @return void
95
     */
96
    protected function createFile($dummySource, $destinationPath)
97
    {
98
        $pluralModel = str_plural($this->model);
99
        $dummyRepository = $this->filesystem->get($dummySource);
100
        $repositoryContent = str_replace(['Dummy', 'Dummies'], [$this->model, $pluralModel], $dummyRepository);
101
        $this->filesystem->put($dummySource, $repositoryContent);
102
        $this->filesystem->copy($dummySource, $destinationPath);
103
        $this->filesystem->put($dummySource, $dummyRepository);
104
    }
105
106
    /**
107
     * Create all required folders
108
     * 
109
     * @return void
110
     */
111
    protected function createFolders($baseFolder)
112
    {
113
        // get plural from model name
114
        $pluralModel = str_plural($this->model);
115
         // create container folder
116
        $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}"));
117
         // add requests folder
118
        $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Requests"));
119
         // add repositories folder
120
        $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Repositories/"));
121
         // add Interfaces folder
122
        $this->filesystem->makeDirectory(app_path($baseFolder."/{$pluralModel}/Repositories/Interfaces"));
123
    }
124
}
125