Passed
Push — master ( edca9d...11ff75 )
by AHMED JOHARI
03:00
created

ComponentGenerator::handle()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 47
rs 9.392
cc 4
nc 1
nop 2
1
<?php
2
3
namespace Joesama\Pintu\Consoles;
4
5
use ReflectionClass;
6
use Illuminate\Support\Arr;
7
use PhpSchool\CliMenu\CliMenu;
0 ignored issues
show
Bug introduced by
The type PhpSchool\CliMenu\CliMenu was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Console\Command;
9
use PhpSchool\CliMenu\MenuStyle;
0 ignored issues
show
Bug introduced by
The type PhpSchool\CliMenu\MenuStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use PhpSchool\CliMenu\Input\Text;
0 ignored issues
show
Bug introduced by
The type PhpSchool\CliMenu\Input\Text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhpSchool\CliMenu\Input\InputIO;
0 ignored issues
show
Bug introduced by
The type PhpSchool\CliMenu\Input\InputIO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Illuminate\Filesystem\Filesystem;
13
use Joesama\Pintu\Components\Manager;
14
use Illuminate\Support\ServiceProvider;
15
use PhpSchool\CliMenu\Action\GoBackAction;
0 ignored issues
show
Bug introduced by
The type PhpSchool\CliMenu\Action\GoBackAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
0 ignored issues
show
Bug introduced by
The type PhpSchool\CliMenu\Builder\CliMenuBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Illuminate\Contracts\Foundation\Application;
18
19
class ComponentGenerator extends Command
20
{
21
    private $serviceProvider;
0 ignored issues
show
introduced by
The private property $serviceProvider is not used, and could be removed.
Loading history...
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'pintu';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create package component & routing configuration file.';
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle(Application $app, Filesystem $file)
42
    {
43
        $placeHolder = 'Joesama\\Pintu\\PintuProvider';
44
45
        $console = $this;
46
47
        $itemCallable = function (CliMenu $menu) use ($placeHolder, $console, $file) {
48
            $successStyle = (new MenuStyle)
49
                ->setBg('254')
50
                ->setFg('166');
51
52
            $result = $menu->askText($successStyle)
53
                ->setPromptText('Type service provider namespace. [esc] to exit')
54
                ->setPlaceholderText($placeHolder)
55
                ->setValidationFailedText('Please type full qualified service provider namespace')
56
                ->setValidator(function ($provider) use ($placeHolder, $console, $file) {
0 ignored issues
show
Unused Code introduced by
The import $file is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
57
                    if (!$console->fileIsExist($provider)) {
58
                        $this->setValidationFailedText("Class {$provider} must be exist");
0 ignored issues
show
Bug introduced by
The method setValidationFailedText() does not exist on Joesama\Pintu\Consoles\ComponentGenerator. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

58
                        $this->/** @scrutinizer ignore-call */ 
59
                               setValidationFailedText("Class {$provider} must be exist");
Loading history...
59
                        return false;
60
                    }
61
62
                    return $provider !== $placeHolder;
63
                })
64
                ->ask();
65
66
            $providerOption = $result->fetch();
67
68
            if (strlen($providerOption) > 0 && $providerOption !== $placeHolder) {
69
                $console->makeComponentFile($menu, $file, $result->fetch());
70
            }
71
        };
72
73
        $menu = ($builder = new CliMenuBuilder)
74
            ->setWidth($builder->getTerminal()->getWidth() - 2 * 2)
75
            ->setMarginAuto()
76
            ->setPadding(2, 4)
77
            ->setTitle('Component File Generator')
78
            ->setTitleSeparator('=')
79
            ->setBackgroundColour('166')
80
            ->setForegroundColour('254')
81
            ->setExitButtonText("Leave console")
82
            ->setUnselectedMarker('❅ ')
83
            ->setSelectedMarker('> ')
84
            ->addItem('Create component file', $itemCallable)
85
            ->build();
86
87
        $menu->open();
88
    }
89
90
    /**
91
     * Check service provider file exist.
92
     *
93
     * @param string $provider
94
     *
95
     * @return bool
96
     */
97
    private function fileIsExist(string $provider): bool
98
    {
99
        return class_exists($provider) ? true : false;
100
    }
101
102
    /**
103
     * Generate component file.
104
     *
105
     * @param CliMenu $menu
106
     * @param Filesystem $file
107
     * @param string $providerOption
108
     *
109
     * @return void
110
     */
111
    private function makeComponentFile(CliMenu $menu, Filesystem $file, string $providerOption)
112
    {
113
        $provider = new ReflectionClass($providerOption);
114
115
        $manager = new Manager($provider);
116
117
        $filePath = $manager->getComponentFilePath();
118
119
        $stub = $file->get($manager->getComponentFileStub());
120
121
        $successStyle = (new MenuStyle)
122
            ->setBg('254')
123
            ->setFg('166');
124
125
        if ($file->exists($filePath)) {
126
            $result = $menu->askText($successStyle)
127
                ->setPromptText("Component file {$filePath} already exists!. Want to overwrite?")
128
                ->setPlaceholderText(' Y / N ')
129
                ->setValidationFailedText('Please choose either Y / N')
130
                ->setValidator(function ($force) {
131
                    return in_array(strtolower($force), ['y', 'n']);
132
                })->ask();
133
134
            $force = $result->fetch();
135
136
            if (strtolower($force) === strtolower('N')) {
137
                $menu->confirm('Component file are remained same!!!', $successStyle)->display('OK!');
138
            } elseif (strtolower($force) === strtolower('Y')) {
139
                $file->copy($filePath, \str_replace('component.php', date('dmyHis') . '.php', $filePath));
140
141
                $this->copyStubToLocation($file, $filePath, $stub);
142
143
                $menu->confirm('Component file successfully overwritten!!!', $successStyle)->display('OK!');
144
            }
145
        } else {
146
            $this->copyStubToLocation($file, $filePath, $stub);
147
148
            $menu->confirm('Component file successfully created!!!', $successStyle)->display('OK!');
149
        }
150
    }
151
152
    /**
153
     * Copy stubs to location.
154
     *
155
     * @param Filesystem $file
156
     * @param string $filePath
157
     * @param $stub
158
     *
159
     * @return void
160
     */
161
    private function copyStubToLocation(Filesystem $file, string $filePath, $stub)
162
    {
163
        if (!$file->isDirectory(dirname($filePath))) {
164
            $file->makeDirectory(dirname($filePath), 0655, true, true);
165
        }
166
167
        $file->put($filePath, $stub);
168
    }
169
}
170