Completed
Push — master ( 00bb2a...06ca93 )
by Cristian
04:59
created

AddCustomRouteContent::handle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 27
nc 4
nop 0
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base\app\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
use Symfony\Component\Process\Exception\ProcessFailedException;
8
use Symfony\Component\Process\Process;
9
10
class AddCustomRouteContent extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'backpack:base:add-custom-route
18
                                {code : HTML/PHP code that registers a route. Use either single quotes or double quotes. Never both. }';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Add HTML/PHP code to the routes/backpack/custom.php file';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $path = 'routes/backpack/custom.php';
45
        $disk_name = config('backpack.base.root_disk_name');
46
        $disk = Storage::disk($disk_name);
47
        $code = $this->argument('code');
48
49
        if ($disk->exists($path)) {
50
            $old_file_content = Storage::disk($disk_name)->get($path);
51
52
            // insert the given code before the file's last line
53
            $file_lines = explode(PHP_EOL, $old_file_content);
54
            $number_of_lines = count($file_lines);
55
            $file_lines[$number_of_lines] = $file_lines[$number_of_lines - 1];
56
            $file_lines[$number_of_lines - 1] = '    '.$code;
57
            $new_file_content = implode(PHP_EOL, $file_lines);
58
59
            if ($disk->put($path, $new_file_content)) {
60
                $this->info('Successfully added code to '.$path);
61
            } else {
62
                $this->error('Could not write to file: '.$path);
63
            }
64
        } else {
65
            $command = 'php artisan vendor:publish --provider="Backpack\Base\BaseServiceProvider" --tag=custom_routes';
66
67
            $process = new Process($command, null, null, null, 300, null);
0 ignored issues
show
Unused Code introduced by
The call to Process::__construct() has too many arguments starting with null.

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...
68
69
            $process->run(function ($type, $buffer) {
70
                if (Process::ERR === $type) {
71
                    $this->line($buffer);
72
                } else {
73
                    $this->line($buffer);
74
                }
75
            });
76
77
            // executes after the command finishes
78
            if (!$process->isSuccessful()) {
79
                throw new ProcessFailedException($process);
80
            }
81
82
            $this->handle();
83
        }
84
    }
85
}
86