MakeMenu   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 176
Duplicated Lines 11.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 21
loc 176
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 9 1
A mountpoint() 0 4 1
A destinationFile() 0 4 1
A createTmpFileWithMenu() 0 6 1
A getMenuCode() 14 14 2
A getPath() 0 4 1
A method() 7 7 2
A name() 0 7 2
A processInput() 0 4 1
A postActions() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
Duplication introduced by Sergi Tur Badenas
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
Duplication introduced by Sergi Tur Badenas
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) {
0 ignored issues
show
Bug introduced by Sergi Tur Badenas
It seems like you are loosely comparing $this->argument('name') of type string|array|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
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();
180
    }
181
182
    /**
183
     * Execute post actions (if exists)
184
     */
185
    protected function postActions()
186
    {
187
        //
188
    }
189
}
190