Completed
Push — master ( bd4222...e5ba92 )
by Arjay
13:31
created

WidgetMakeCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 84
rs 10
wmc 6
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildClass() 0 8 1
A createJsonConfig() 0 17 2
A getJsonPath() 0 4 1
A getJsonStubPath() 0 6 1
A replaceFQCN() 0 6 1
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