Completed
Push — develop ( 9af3f8...36c8fb )
by Abdelrahman
12:51
created

TerminalController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 119
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B authorizeResource() 0 15 5
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\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 = 'terminal';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $resourceAbilityMap = [
24
        'index' => 'run',
25
        'execute' => 'run',
26
    ];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected $resourceMethodsWithoutModels = [
32
        'execute',
33
    ];
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function authorizeResource($model, $parameter = null, array $options = [], $request = null): void
39
    {
40
        $middleware = [];
41
        $parameter = $parameter ?: Str::snake(class_basename($model));
42
43
        foreach ($this->mapResourceAbilities() as $method => $ability) {
44
            $modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter;
45
46
            $middleware["can:{$ability}-{$modelName},{$modelName}"][] = $method;
47
        }
48
49
        foreach ($middleware as $middlewareName => $methods) {
50
            $this->middleware($middlewareName, $options)->only($methods);
51
        }
52
    }
53
54
    /**
55
     * Show terminal index.
56
     *
57
     * @param \Illuminate\Http\Request          $request
58
     * @param \Cortex\Console\Services\Terminal $terminal
59
     *
60
     * @return \Illuminate\View\View
61
     */
62
    public function index(Request $request, Terminal $terminal)
63
    {
64
        $token = null;
65
66
        if ($request->hasSession() === true) {
67
            $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...
68
        }
69
70
        $terminal->call('list --ansi');
71
        $options = json_encode([
72
            'username' => 'LARAVEL',
73
            'hostname' => php_uname('n'),
74
            'os' => PHP_OS,
75
            'csrfToken' => $token,
76
            'helpInfo' => $terminal->output(),
77
            'basePath' => app()->basePath(),
78
            'environment' => app()->environment(),
79
            'version' => app()->version(),
80
            'endpoint' => route('adminarea.console.terminal.execute'),
81
            'interpreters' => [
82
                'mysql' => 'mysql',
83
                'artisan tinker' => 'tinker',
84
                'tinker' => 'tinker',
85
            ],
86
            'confirmToProceed' => [
87
                'artisan' => [
88
                    'migrate',
89
                    'migrate:install',
90
                    'migrate:refresh',
91
                    'migrate:reset',
92
                    'migrate:rollback',
93
                    'db:seed',
94
                ],
95
            ],
96
        ]);
97
98
        return view('cortex/console::adminarea.pages.terminal', compact('options'));
99
    }
100
101
    /**
102
     * Process the form for store/update of the given resource.
103
     *
104
     * @param \Illuminate\Http\Request $request
105
     *
106
     * @return \Illuminate\Http\JsonResponse
107
     */
108
    public function execute(Terminal $terminal, Request $request)
109
    {
110
        $error = $terminal->call($request->get('command'));
111
112
        return response()->json([
113
            'jsonrpc' => $request->get('jsonrpc'),
114
            'id' => $request->get('id'),
115
            'result' => $terminal->output(),
116
            'error' => $error,
117
        ]);
118
    }
119
120
    /**
121
     * Render exception.
122
     *
123
     * @param \Exception $exception
124
     *
125
     * @return \Illuminate\View\View
126
     */
127
    protected function renderException(Exception $exception)
128
    {
129
        return view('cortex/console::adminarea.pages.error', ['message' => $exception->getMessage()]);
130
    }
131
}
132