Completed
Push — develop ( 862e2d...5e18c4 )
by Abdelrahman
01:28
created

TerminalController::authorizeResource()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 12
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Console\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Illuminate\Support\Str;
9
use Illuminate\Http\Request;
10
use Cortex\Console\Services\Terminal;
11
use Cortex\Foundation\Http\Controllers\AuthorizedController;
12
13
class TerminalController extends AuthorizedController
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $resource = 'run-terminal';
19
20
    /**
21
     * Show terminal index.
22
     *
23
     * @param \Illuminate\Http\Request          $request
24
     * @param \Cortex\Console\Services\Terminal $terminal
25
     *
26
     * @return \Illuminate\View\View
27
     */
28
    public function index(Request $request, Terminal $terminal)
29
    {
30
        $token = null;
31
32
        if ($request->hasSession() === true) {
33
            $token = $request->session()->token();
0 ignored issues
show
Bug introduced by
The method token() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
        }
35
36
        $terminal->call('list --ansi');
37
        $options = json_encode([
38
            'username' => 'LARAVEL',
39
            'hostname' => php_uname('n'),
40
            'os' => PHP_OS,
41
            'csrfToken' => $token,
42
            'helpInfo' => $terminal->output(),
43
            'basePath' => app()->basePath(),
44
            'environment' => app()->environment(),
45
            'version' => app()->version(),
46
            'endpoint' => route('adminarea.console.terminal.execute'),
47
            'interpreters' => [
48
                'mysql' => 'mysql',
49
                'artisan tinker' => 'tinker',
50
                'tinker' => 'tinker',
51
            ],
52
            'confirmToProceed' => [
53
                'artisan' => [
54
                    'migrate',
55
                    'migrate:install',
56
                    'migrate:refresh',
57
                    'migrate:reset',
58
                    'migrate:rollback',
59
                    'db:seed',
60
                ],
61
            ],
62
        ]);
63
64
        return view('cortex/console::adminarea.pages.terminal', compact('options'));
65
    }
66
67
    /**
68
     * Process the form for store/update of the given resource.
69
     *
70
     * @param \Illuminate\Http\Request $request
71
     *
72
     * @return \Illuminate\Http\JsonResponse
73
     */
74
    public function execute(Terminal $terminal, Request $request)
75
    {
76
        $error = $terminal->call($request->get('command'));
77
78
        return response()->json([
79
            'jsonrpc' => $request->get('jsonrpc'),
80
            'id' => $request->get('id'),
81
            'result' => $terminal->output(),
82
            'error' => $error,
83
        ]);
84
    }
85
86
    /**
87
     * Render exception.
88
     *
89
     * @param \Exception $exception
90
     *
91
     * @return \Illuminate\View\View
92
     */
93
    protected function renderException(Exception $exception)
94
    {
95
        return view('cortex/console::adminarea.pages.error', ['message' => $exception->getMessage()]);
96
    }
97
}
98