Completed
Push — master ( db34bb...7c76f5 )
by recca
06:43 queued 04:41
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 Recca0120\Terminal\Kernel;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Filesystem\Filesystem;
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)
36
    {
37 2
        $this->request = $request;
38 2
        $this->responseFactory = $responseFactory;
39 2
    }
40
41
    /**
42
     * index.
43
     *
44
     * @param \Recca0120\Terminal\Kernel $kernel
45
     * @param string $view
46
     * @return \Illuminate\Http\Response
47
     */
48 1
    public function index(Kernel $kernel, $view = 'index')
49
    {
50 1
        $token = null;
51 1
        if ($this->request->hasSession() === true) {
52 1
            $token = $this->request->session()->token();
53 1
        }
54
55 1
        $kernel->call('list --ansi');
56 1
        $options = json_encode(array_merge($kernel->getConfig(), [
57 1
            'csrfToken' => $token,
58 1
            'helpInfo' => $kernel->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\Kernel $kernel
69
     * @return \Illuminate\Http\JsonResponse
70
     */
71 1
    public function endpoint(Kernel $kernel)
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 = $kernel->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' => $kernel->output(),
86 1
            'error' => $error,
87 1
        ]);
88
    }
89
90
    /**
91
     * media.
92
     *
93
     * @param \Illuminate\Filesystem\Filesystem $files
94
     * @param string $file
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function media(Filesystem $files, $file)
98
    {
99
        $filename = __DIR__.'/../../../public/'.$file;
100
        $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript';
101
        $lastModified = $files->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