Completed
Push — master ( b1b281...32f961 )
by Sergi Tur
15s
created

src/Console/MakeMenu.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Acacha\AdminLTETemplateLaravel\Console;
4
5
use Acacha\AdminLTETemplateLaravel\Console\Menus\RegularMenu;
6
use Acacha\AdminLTETemplateLaravel\Console\Routes\GeneratesCode;
7
use Acacha\Filesystem\Compiler\StubFileCompiler;
8
use Acacha\Filesystem\Filesystem;
9
use Illuminate\Console\Command;
10
11
/**
12
 * Class MakeMenu.
13
 */
14
class MakeMenu extends Command
15
{
16
17
    /**
18
     * Path to menu file.
19
     *
20
     * @var string
21
     */
22
    protected $menu_path = 'config/menu.php';
23
24
    /**
25
     * Compiler for stub file.
26
     *
27
     * @var StubFileCompiler
28
     */
29
    protected $compiler;
30
31
    /**
32
     * Compiler for stub file.
33
     *
34
     * @var Filesystem
35
     */
36
    protected $filesystem;
37
38
    /**
39
     * @var array
40
     */
41
    protected static $lookup = [
42
        'regular' => RegularMenu::class,
43
//        'another' => AnotherMenu::class,
44
    ];
45
46
    /**
47
     * The name and signature of the console command.
48
     */
49
    protected $signature = 'make:menu {link : The menu link} {name? : The menu name} 
50
        {--t|type=regular : Type of menu to create (regular,todo)}';
51
52
    /**
53
     * The console command description.
54
     *
55
     * @var string
56
     */
57
    protected $description = 'Insert a menu to config/menu file';
58
59
    /**
60
     * AdminLTERoute constructor.
61
     *
62
     * @param StubFileCompiler $compiler
63
     * @param Filesystem $filesystem
64
     */
65
    public function __construct(StubFileCompiler $compiler, Filesystem $filesystem)
66
    {
67
        parent::__construct();
68
        $this->compiler = $compiler;
69
        $this->filesystem = $filesystem;
70
    }
71
72
    /**
73
     * Execute the console command.
74
     */
75
    public function handle()
76
    {
77
        $this->processInput();
78
        $tmpfile = $this->createTmpFileWithMenu();
79
        $path = $this->getPath($tmpfile);
80
        add_file_into_file($this->mountpoint(), $path, $dstFile = $this->destinationFile());
81
        $this->info('Menu ' . $this->name() . ' added to ' .  $dstFile . '.');
82
        $this->postActions();
83
    }
84
85
    /**
86
     * Get mountpoint.
87
     *
88
     * @return string
89
     */
90
    protected function mountpoint()
91
    {
92
        return '#adminlte_menu';
93
    }
94
95
    /**
96
     * Destination route file.
97
     *
98
     * @return string
99
     */
100
    protected function destinationFile()
101
    {
102
        return base_path($this->menu_path);
103
    }
104
105
    /**
106
     * Crete tmp file with route to add.
107
     *
108
     * @return mixed
109
     */
110
    protected function createTmpFileWithMenu()
111
    {
112
        $temp = tmpfile();
113
        fwrite($temp, $this->getMenuCode());
114
        return $temp;
115
    }
116
117
    /**
118
     * Get route code to insert depending on type.
119
     *
120
     * @return mixed
121
     */
122 View Code Duplication
    protected function getMenuCode()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $type = $this->option('type');
125
        $class = isset(static::$lookup[$type])
126
            ? static::$lookup[$type]
127
            : RegularMenu::class;
128
        /** @var GeneratesCode $route */
129
        $route = new $class($this->compiler, $this->filesystem);
130
        $route->setReplacements([
131
            $this->argument('link'),
132
            $this->name()
133
        ]);
134
        return $route->code();
135
    }
136
137
    /**
138
     * Get path from file resource.
139
     *
140
     * @param $tmpfile
141
     * @return mixed
142
     */
143
    protected function getPath($tmpfile)
144
    {
145
        return stream_get_meta_data($tmpfile)['uri'];
146
    }
147
148
    /**
149
     * Get method.
150
     *
151
     * @return string
152
     */
153 View Code Duplication
    protected function method()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        if (strtolower($this->option('method')) == 'head') {
156
            return 'get';
157
        }
158
        return strtolower($this->option('method'));
159
    }
160
161
    /**
162
     * Get the link name.
163
     *
164
     * @return array|string
165
     */
166
    protected function name()
167
    {
168
        if ($this->argument('name') != null) {
169
            return $this->argument('name');
170
        }
171
        return ucfirst($this->argument('link'));
172
    }
173
174
    /**
175
     * Process input.
176
     */
177
    protected function processInput()
178
    {
179
        //        $this->validateMethod();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
180
    }
181
182
    /**
183
     * Execute post actions (if exists)
184
     */
185
    protected function postActions()
186
    {
187
        //
188
    }
189
}
190