Completed
Push — master ( e5ba92...7a1e6c )
by Arjay
13:54
created

WidgetMakeCommand::createView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
eloc 10
c 2
b 0
f 2
nc 2
nop 0
dl 0
loc 17
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Console;
4
5
use Arrilot\Widgets\Console\WidgetMakeCommand as ArrilotWidgetMakeCommand;
6
7
class WidgetMakeCommand extends ArrilotWidgetMakeCommand
8
{
9
    /**
10
     * The console command description.
11
     *
12
     * @var string
13
     */
14
    protected $description = 'Create a new YajraCMS widget.';
15
16
    /**
17
     * Build the class with the given name.
18
     *
19
     * @param string $name
20
     * @return string
21
     */
22
    protected function buildClass($name)
23
    {
24
        $stub = parent::buildClass($name);
25
26
        $this->createJsonConfig($name);
27
28
        return $stub;
29
    }
30
31
    /**
32
     * Create json config file for widget details.
33
     *
34
     * @param string $name
35
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
36
     */
37
    protected function createJsonConfig($name)
38
    {
39
        if ($this->files->exists($path = $this->getJsonPath())) {
40
            $this->error('Widget json config already exists!');
41
42
            return;
43
        }
44
45
        $stub = $this->files->get($this->getJsonStubPath());
46
        $stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
47
        $stub = $this->replaceFQCN($stub, $name);
48
        $stub = $this->replaceView($stub);
49
50
        $this->files->put($path, $stub);
51
52
        $this->info('Json config created successfully.');
53
    }
54
55
    /**
56
     * Get the json stub file for the generator.
57
     *
58
     * @return string
59
     */
60
    protected function getJsonPath()
61
    {
62
        return $this->laravel->basePath() . '/app/Widgets/' . $this->argument('name') . '.json';
63
    }
64
65
    /**
66
     * Get json stub path.
67
     *
68
     * @return string
69
     */
70
    protected function getJsonStubPath()
71
    {
72
        $stubPath = $this->laravel->make('config')->get('laravel-widgets.widget_json_stub');
73
74
        return $this->laravel->basePath() . '/' . $stubPath;
75
    }
76
77
    /**
78
     * Replace class with FQCN.
79
     *
80
     * @param string $stub
81
     * @param string $name
82
     * @return string
83
     */
84
    protected function replaceFQCN($stub, $name)
85
    {
86
        $fqcn = str_replace("\\", "\\\\", $name);
87
88
        return str_replace('{{fqcn}}', $fqcn, $stub);
89
    }
90
91
    /**
92
     * Create a new view file for the widget.
93
     * return void
94
     */
95
    protected function createView()
96
    {
97
        if ($this->files->exists($path = $this->getViewPath())) {
98
            $this->error('View already exists!');
99
100
            return;
101
        }
102
103
        $this->makeDirectory($path);
104
        $view = $this->files->get($this->getViewStub('view'));
0 ignored issues
show
Bug introduced by
The method getViewStub() does not seem to exist on object<Yajra\CMS\Console\WidgetMakeCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
        $this->files->put($path, $view);
106
107
        $form = $this->files->get($this->getViewStub('form'));
0 ignored issues
show
Bug introduced by
The method getViewStub() does not seem to exist on object<Yajra\CMS\Console\WidgetMakeCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        $this->files->put($this->getViewPath('_form'), $form);
0 ignored issues
show
Unused Code introduced by
The call to WidgetMakeCommand::getViewPath() has too many arguments starting with '_form'.

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...
109
110
        $this->info('Views successfully created.');
111
    }
112
}
113