BabelController::installExtension()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Babel\ExtensionModel;
7
use Encore\Admin\Layout\Column;
8
use Encore\Admin\Layout\Content;
9
use Encore\Admin\Layout\Row;
10
use Illuminate\Support\Facades\Redirect;
11
use Illuminate\Support\Str;
12
13
class BabelController extends Controller
14
{
15
    /**
16
     * Show the MarketSpace Page.
17
     *
18
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Admin\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    public function index(Content $content)
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

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

20
    public function index(/** @scrutinizer ignore-unused */ Content $content)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        return redirect()->route('admin.babel.installed');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.babel.installed') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
23
    }
24
25
    /**
26
     * Show the Installed Page.
27
     *
28
     * @return Response
29
     */
30
    public function installed(Content $content)
31
    {
32
        return $content
0 ignored issues
show
Bug Best Practice introduced by
The expression return $content->header(...ion(...) { /* ... */ }) returns the type Encore\Admin\Layout\Content which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
33
            ->header('Installed Babel Extension')
34
            ->description('Manage your installed Babel Extension')
35
            ->row(function(Row $row) {
36
                $row->column(12, function(Column $column) {
37
                    $column->append(Self::installedView());
38
                });
39
            });
40
    }
41
42
    /**
43
     * Show the MarketSpace Page.
44
     *
45
     * @return Response
46
     */
47
    public function marketspace(Content $content)
48
    {
49
        return $content
0 ignored issues
show
Bug Best Practice introduced by
The expression return $content->header(...ion(...) { /* ... */ }) returns the type Encore\Admin\Layout\Content which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
50
            ->header('Babel Marketspace')
51
            ->description('Find extensions from marketspace')
52
            ->row(function(Row $row) {
53
                $row->column(12, function(Column $column) {
54
                    $column->append(Self::marketspaceView());
55
                });
56
            });
57
    }
58
59
    /**
60
     * Show the MarketSpace Detail Page.
61
     *
62
     * @return Response
63
     */
64
    public function detail($code, Content $content)
65
    {
66
        return $content
0 ignored issues
show
Bug Best Practice introduced by
The expression return $content->header(...ion(...) { /* ... */ }) returns the type Encore\Admin\Layout\Content which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
67
            ->header("Extension: $code")
68
            ->description('Details about this extension')
69
            ->row(function(Row $row) use ($code) {
70
                $row->column(12, function(Column $column) use ($code) {
71
                    $column->append(Self::marketspaceDetailView($code));
72
                });
73
            });
74
    }
75
76
    /**
77
     * Show the Extension Update Page.
78
     *
79
     * @return Response
80
     */
81
    public function update($extension, Content $content)
82
    {
83
        return $this->execute('update', $extension, $content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute('u..., $extension, $content) returns the type Encore\Admin\Layout\Content which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
84
    }
85
86
    /**
87
     * Show the Extension Install Page.
88
     *
89
     * @return Response
90
     */
91
    public function install($extension, Content $content)
92
    {
93
        return $this->execute('install', $extension, $content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute('i..., $extension, $content) returns the type Encore\Admin\Layout\Content which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
94
    }
95
96
    public function execute($command, $extension, Content $content)
97
    {
98
        return $content
99
            ->header(Str::title($command)." $extension")
100
            ->row(function(Row $row) use ($extension) {
101
                $row->column(12, function(Column $column) use ($extension) {
102
                    $column->append(Self::executingView($extension));
103
                });
104
            });
105
    }
106
107
    public function updateExtension($extension, Content $content)
108
    {
109
        $this->operateExtension('update', $extension, $content);
110
    }
111
112
    public function installExtension($extension, Content $content)
113
    {
114
        $this->operateExtension('install', $extension, $content);
115
    }
116
117
    public function operateExtension($command, $extension, Content $content)
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function operateExtension($command, $extension, /** @scrutinizer ignore-unused */ Content $content)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
        self::executeArtisan("babel:$command $extension --no-interaction");
120
    }
121
122
    private static function installedView()
123
    {
124
        $installedExtensionList=ExtensionModel::localList();
125
126
        return view('admin::babel.installed', [
127
            'installedExtensionList'=>$installedExtensionList
128
        ]);
129
    }
130
131
    private static function marketspaceView()
132
    {
133
        $extensionList=ExtensionModel::list();
134
135
        if (empty($extensionList)) {
136
            return view('admin::babel.empty');
137
        }
138
139
        return view('admin::babel.marketspace', [
140
            'extensionList'=>$extensionList
141
        ]);
142
    }
143
144
    private static function marketspaceDetailView($code)
145
    {
146
        $details=ExtensionModel::remoteDetail($code);
147
148
        if (empty($details)) {
149
            return view('admin::babel.empty');
150
        }
151
152
        return view('admin::babel.detail', [
153
            'details'=>$details
154
        ]);
155
    }
156
157
    private static function executingView($extension)
158
    {
159
        $details=ExtensionModel::remoteDetail($extension);
160
161
        if (empty($details)) {
162
            return view('admin::babel.empty');
163
        }
164
165
        return view('admin::babel.execute', [
166
            'extension'=>$extension
167
        ]);
168
    }
169
170
    private static function executeArtisan($command)
171
    {
172
        $fp=popen('php "'.base_path('artisan').'" '.$command, "r");
173
        while ($b=fgets($fp, 2048)) {
174
            echo str_pad(json_encode([
175
                "ret"=>200,
176
                "desc"=>"Succeed",
177
                "data"=>[
178
                    "message"=>$b
179
                ]
180
            ])."\n", 4096);
181
            @ob_flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ob_flush(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

181
            /** @scrutinizer ignore-unhandled */ @ob_flush();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
182
            flush();
183
        }
184
185
        pclose($fp);
186
    }
187
}
188