Completed
Push — master ( 71c78b...6eeaca )
by recca
10:24 queued 05:45
created

TerminalController::endpoint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 18
ccs 14
cts 14
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Http\Controllers;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Filesystem\Filesystem;
9
use Recca0120\Terminal\TerminalManager;
10
use Illuminate\Contracts\Foundation\Application;
11
use Illuminate\Contracts\Routing\ResponseFactory;
12
13
class TerminalController extends Controller
14
{
15
    /**
16
     * $request.
17
     *
18
     * @var \Illuminate\Http\Request
19
     */
20
    protected $request;
21
22
    /**
23
     * $responseFactory.
24
     *
25
     * @var \Illuminate\Contracts\Routing\ResponseFactory
26
     */
27
    protected $responseFactory;
28
29
    /**
30
     * __construct.
31
     *
32
     * @param \Illuminate\Http\Request         $request
33
     * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
34
     */
35 2
    public function __construct(Request $request, ResponseFactory $responseFactory)
1 ignored issue
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
36
    {
37 2
        $this->request = $request;
38 2
        $this->responseFactory = $responseFactory;
39 2
    }
40
41
    /**
42
     * index.
43
     *
44
     * @param \Recca0120\Terminal\TerminalManager   $terminalManger
45
     * @param string                                $view
46
     * @return \Illuminate\Http\Response
47
     */
48 1
    public function index(TerminalManager $terminalManger, $view = 'index')
49
    {
50 1
        $token = null;
51 1
        if ($this->request->hasSession() === true) {
52 1
            $token = $this->request->session()->token();
1 ignored issue
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...
53 1
        }
54
55 1
        $terminalManger->call('list --ansi');
56 1
        $options = json_encode(array_merge($terminalManger->getConfig(), [
57 1
            'csrfToken' => $token,
58 1
            'helpInfo' => $terminalManger->output(),
59 1
        ]));
60 1
        $id = ($view === 'panel') ? Str::random(30) : null;
61
62 1
        return $this->responseFactory->view('terminal::'.$view, compact('options', 'id'));
63
    }
64
65
    /**
66
     * rpc response.
67
     *
68
     * @param \Recca0120\Terminal\TerminalManager   $terminalManger
69
     * @return \Illuminate\Http\JsonResponse
70
     */
71 1
    public function endpoint(TerminalManager $terminalManger)
72
    {
73 1
        if ($this->request->hasSession() === true) {
74 1
            $session = $this->request->session();
75 1
            if ($session->isStarted() === true) {
76 1
                $session->save();
77 1
            }
78 1
        }
79
80 1
        $error = $terminalManger->call($this->request->get('command'));
81
82 1
        return $this->responseFactory->json([
83 1
            'jsonrpc' => $this->request->get('jsonrpc'),
84 1
            'id' => $this->request->get('id'),
85 1
            'result' => $terminalManger->output(),
86 1
            'error' => $error,
87 1
        ]);
88
    }
89
90
    /**
91
     * media.
92
     *
93
     * @param \Illuminate\Filesystem\Filesystem $filesystem
94
     * @param string                            $file
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function media(Filesystem $filesystem, $file)
98
    {
99
        $filename = __DIR__.'/../../../public/'.$file;
100
        $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript';
101
        $lastModified = $filesystem->lastModified($filename);
102
        $eTag = sha1_file($filename);
103
        $headers = [
104
            'content-type' => $mimeType,
105
            'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT',
106
        ];
107
108
        if (@strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified ||
109
            trim($this->request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag
110
        ) {
111
            $response = $this->responseFactory->make(null, 304, $headers);
112
        } else {
113
            $response = $this->responseFactory->stream(function () use ($filename) {
114
                $out = fopen('php://output', 'wb');
115
                $file = fopen($filename, 'rb');
116
                stream_copy_to_stream($file, $out, filesize($filename));
117
                fclose($out);
118
                fclose($file);
119
            }, 200, $headers);
120
        }
121
122
        return $response->setEtag($eTag);
123
    }
124
}
125