Passed
Branch master (ffb5df)
by Michele
03:52
created

WebArtisanCommandController::run()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 21
rs 8.8333
1
<?php
2
3
namespace Micheledamo\LaravelWebArtisan\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Artisan;
7
use Symfony\Component\Console\Exception\CommandNotFoundException;
8
9
class WebArtisanCommandController extends WebArtisanBaseController
10
{
11
    /**
12
     * The command string
13
     *
14
     * @var
15
     */
16
    protected $command;
17
18
    /**
19
     * Run the command
20
     *
21
     * @param Request $request
22
     * @return string
23
     */
24
    public function run(Request $request)
25
    {
26
        $this->command = $request->has('command')
27
            ? $request->get('command')
28
            : null;
29
30
        if($this->command) {
31
            $commandPrepared = $this->prepareCommand($this->command);
32
            if(!config('webartisan.use_authentication')
33
                or (config('webartisan.use_authentication') and $this->webartisan->isAuthenticated())) {
34
                    try {
35
                        Artisan::call($commandPrepared);
0 ignored issues
show
Bug introduced by
It seems like $commandPrepared can also be of type string[]; however, parameter $command of Illuminate\Support\Facades\Artisan::call() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
                        Artisan::call(/** @scrutinizer ignore-type */ $commandPrepared);
Loading history...
36
                    }
37
                    catch(\Exception $e) {
38
                        return $this->prepareResultToHtml($e->getMessage(), 'error');
39
                    }
40
            }
41
            else {
42
                return $this->prepareResultToHtml('Please, authenticate yourself before to start using Web Artisan.', 'error');
43
            }
44
            return $this->prepareResultToHtml(Artisan::output(), 'success');
45
        }
46
    }
47
48
    /**
49
     * Prepare command for Artisan
50
     *
51
     * @param $command
52
     * @return mixed
53
     */
54
    public function prepareCommand($command)
55
    {
56
        $command = $this->clearPhpArtisan($command);
57
        return $command;
58
    }
59
60
    /**
61
     * Remove "php artisan" if present
62
     *
63
     * @param $command
64
     * @return string|string[]
65
     */
66
    public function clearPhpArtisan($command)
67
    {
68
        return str_replace("php artisan ", "", $command);
69
    }
70
71
    /**
72
     * Convert to HTML
73
     * and present with type colors
74
     *
75
     * @param $string
76
     * @param $type
77
     * @return string|string[]
78
     */
79
    public function prepareResultToHtml($string, $type)
80
    {
81
        $string = str_replace("\n", '<br>',
82
            str_replace("\r", '<br>',
83
                str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;',
84
                    str_replace(" ", '&nbsp;',
85
                        $string))));
86
87
        switch ($type) {
88
            case "error": { $string = '<span class="webartisan__window__results__error">' . $string . '</span>'; }break;
89
            case "success": { $string = '<span class="webartisan__window__results__success">' . $string . '</span>'; }break;
90
        }
91
92
        return $string;
93
    }
94
}