Passed
Push — master ( a2d6f0...8d3605 )
by AHMED JOHARI
06:58
created

ComponentGenerator::forceTriggered()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
rs 10
cc 4
nc 3
nop 2
1
<?php
2
namespace Joesama\Pintu\Consoles;
3
4
use Illuminate\Support\Arr;
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Joesama\Pintu\Components\Manager;
8
use Illuminate\Support\ServiceProvider;
9
use Illuminate\Contracts\Foundation\Application;
10
11
class ComponentGenerator extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'pintu 
19
        { provider : Fully qualified service provider name }
20
        { --f|force : Force to recreate component file. }
21
    ';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Define package component & routing';
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return mixed
34
     */
35
    public function handle(Application $app, Filesystem $file)
36
    {
37
        $providerOption = $this->argument('provider');
38
39
        $provider = $this->isServiceProvider($app, $providerOption);
0 ignored issues
show
Bug introduced by
It seems like $providerOption can also be of type null and string[]; however, parameter $providerOption of Joesama\Pintu\Consoles\C...or::isServiceProvider() 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

39
        $provider = $this->isServiceProvider($app, /** @scrutinizer ignore-type */ $providerOption);
Loading history...
40
41
        if (!$this->fileIsExist($providerOption)
0 ignored issues
show
Bug introduced by
It seems like $providerOption can also be of type null and string[]; however, parameter $provider of Joesama\Pintu\Consoles\C...enerator::fileIsExist() 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

41
        if (!$this->fileIsExist(/** @scrutinizer ignore-type */ $providerOption)
Loading history...
42
            &&
43
            ($provider === false) ) {
44
            return false;
45
        }
46
47
        $manager = new Manager($provider);
48
49
        $filePath = $manager->getComponentFilePath();
50
51
        $stub = $file->get($manager->getComponentFileStub());
52
53
        $this->alreadyExist($file, $filePath);
54
55
        $this->forceTriggered($file, $filePath);
56
57
        if (! $file->isDirectory(dirname($filePath))) {
58
            $file->makeDirectory(dirname($filePath), 0655, true, true);
59
        }
60
61
        $file->put($filePath, $stub);
62
63
        $this->line('Component file successfully created!');
64
65
    }
66
67
    /**
68
     * Check service provider file exist.
69
     *
70
     * @param string $provider
71
     *
72
     * @return bool
73
     */
74
    private function fileIsExist(string $provider): bool
75
    {
76
        if (\class_exists($provider)) {
77
            return true;
78
        }
79
80
        $this->error("class {$provider} must be exist");
81
82
        return false;
83
    }
84
85
    /**
86
     * Provider passed is registered provider.
87
     *
88
     * @param [type] $app
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
89
     * @param string $providerOption
90
     *
91
     * @return bool
92
     */
93
    private function isServiceProvider($app, string $providerOption)
94
    {
95
        $provider = Arr::first($app->getProviders($providerOption));
96
97
        if ($provider === null) {
98
            $this->error("class {$providerOption} must be instance of ". ServiceProvider::class);
99
100
            return false;
101
        }
102
103
        return $provider;
104
    }
105
106
    /**
107
     * Validate if component file already exist.
108
     *
109
     * @return void
110
     */
111
    private function alreadyExist($file, $filePath)
112
    {
113
        if (! $this->option('force') && $file->exists($filePath)) {
114
            $this->error(
115
                'Component file already exists!. Please use --force to re-create component file'
116
            );
117
118
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
119
        }
120
    }
121
122
    /**
123
     * Process if --force indicator is used.
124
     *
125
     * @param [type] $file
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
126
     * @param [type] $filePath
127
     *
128
     * @return void
129
     */
130
    private function forceTriggered($file, $filePath)
131
    {
132
        if ($this->option('force') && $file->exists($filePath)) {
133
            $force = $this->choice(
134
                'This will replace current component file. Do you wish to continoue?',
135
                [ 'Y', "N"]
136
            );
137
138
            if ($force === 'N') {
139
                $this->line('Component file are remained same!!!');
140
                
141
                exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
142
            }
143
144
            $file->copy($filePath, \str_replace('component.php', date('dmyHis') . '.php', $filePath));
145
        }
146
    }
147
}
148