Completed
Push — develop ( 553dd4...32e3cc )
by Abdelrahman
01:53
created

TerminalController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 90
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 38 2
A execute() 0 11 1
A renderException() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Console\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Illuminate\Http\Request;
9
use Cortex\Console\Services\Terminal;
10
use Cortex\Foundation\Http\Controllers\AuthorizedController;
11
12
class TerminalController extends AuthorizedController
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $resource = 'terminal';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resourceAbilityMap = [
23
        'index' => 'run',
24
        'execute' => 'run',
25
    ];
26
27
    /**
28
     * Show the form for create/update of the given resource.
29
     *
30
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
31
     */
32
    public function index(Terminal $terminal, Request $request)
33
    {
34
        $token = null;
35
36
        if ($request->hasSession() === true) {
37
            $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...
38
        }
39
40
        $terminal->call('list --ansi');
41
        $options = json_encode([
42
            'username' => 'LARAVEL',
43
            'hostname' => php_uname('n'),
44
            'os' => PHP_OS,
45
            'csrfToken' => $token,
46
            'helpInfo' => $terminal->output(),
47
            'basePath' => app()->basePath(),
48
            'environment' => app()->environment(),
49
            'version' => app()->version(),
50
            'endpoint' => route('adminarea.console.terminal.execute'),
51
            'interpreters' => [
52
                'mysql' => 'mysql',
53
                'artisan tinker' => 'tinker',
54
                'tinker' => 'tinker',
55
            ],
56
            'confirmToProceed' => [
57
                'artisan' => [
58
                    'migrate',
59
                    'migrate:install',
60
                    'migrate:refresh',
61
                    'migrate:reset',
62
                    'migrate:rollback',
63
                    'db:seed',
64
                ],
65
            ],
66
        ]);
67
68
        return view('cortex/console::adminarea.forms.terminal', compact('options'));
69
    }
70
71
    /**
72
     * Process the form for store/update of the given resource.
73
     *
74
     * @param \Illuminate\Http\Request $request
75
     *
76
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
77
     */
78
    public function execute(Terminal $terminal, Request $request)
79
    {
80
        $error = $terminal->call($request->get('command'));
81
82
        return response()->json([
83
            'jsonrpc' => $request->get('jsonrpc'),
84
            'id' => $request->get('id'),
85
            'result' => $terminal->output(),
86
            'error' => $error,
87
        ]);
88
    }
89
90
    /**
91
     * Render exception.
92
     *
93
     * @param \Exception $exception
94
     *
95
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    protected function renderException(Exception $exception)
98
    {
99
        return view('cortex/console::adminarea.forms.error', ['message' => $exception->getMessage()]);
100
    }
101
}
102