Completed
Push — master ( df2488...ce1ba6 )
by recca
02:11
created

TerminalController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 1
b 0
f 1
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\Routing\ResponseFactory;
11
12
class TerminalController extends Controller
13
{
14
    /**
15
     * $request.
16
     *
17
     * @var \Illuminate\Http\Request
18
     */
19
    protected $request;
20
21
    /**
22
     * $responseFactory.
23
     *
24
     * @var \Illuminate\Contracts\Routing\ResponseFactory
25
     */
26
    protected $responseFactory;
27
28
    /**
29
     * __construct.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
33
     */
34 2
    public function __construct(Request $request, ResponseFactory $responseFactory)
35
    {
36 2
        $this->request = $request;
37 2
        $this->responseFactory = $responseFactory;
38 2
    }
39
40
    /**
41
     * index.
42
     *
43
     * @param \Recca0120\Terminal\Kernel $kernel
44
     * @param string $view
45
     * @return \Illuminate\Http\Response
46
     */
47 1
    public function index(Kernel $kernel, $view = 'index')
48
    {
49 1
        $token = null;
50 1
        if ($this->request->hasSession() === true) {
51 1
            $token = $this->request->session()->token();
52 1
        }
53
54 1
        $kernel->call('list --ansi');
55 1
        $options = json_encode(array_merge($kernel->getConfig(), [
56 1
            'csrfToken' => $token,
57 1
            'helpInfo' => $kernel->output(),
58 1
        ]));
59 1
        $id = ($view === 'panel') ? Str::random(30) : null;
60
61 1
        return $this->responseFactory->view('terminal::'.$view, compact('options', 'id'));
62
    }
63
64
    /**
65
     * rpc response.
66
     *
67
     * @param \Recca0120\Terminal\Kernel $kernel
68
     * @return \Illuminate\Http\JsonResponse
69
     */
70 1
    public function endpoint(Kernel $kernel)
71
    {
72 1
        $error = $kernel->call($this->request->get('command'));
73
74 1
        return $this->responseFactory->json([
75 1
            'jsonrpc' => $this->request->get('jsonrpc'),
76 1
            'id' => $this->request->get('id'),
77 1
            'result' => $kernel->output(),
78 1
            'error' => $error,
79 1
        ]);
80
    }
81
82
    /**
83
     * media.
84
     *
85
     * @param \Illuminate\Filesystem\Filesystem $files
86
     * @param string $file
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function media(Filesystem $files, $file)
90
    {
91
        $filename = __DIR__.'/../../../public/'.$file;
92
        $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript';
93
        $lastModified = $files->lastModified($filename);
94
        $eTag = sha1_file($filename);
95
        $headers = [
96
            'content-type' => $mimeType,
97
            'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT',
98
        ];
99
100
        if (@strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified ||
101
            trim($this->request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag
102
        ) {
103
            $response = $this->responseFactory->make(null, 304, $headers);
104
        } else {
105
            $response = $this->responseFactory->stream(function () use ($filename) {
106
                $out = fopen('php://output', 'wb');
107
                $file = fopen($filename, 'rb');
108
                stream_copy_to_stream($file, $out, filesize($filename));
109
                fclose($out);
110
                fclose($file);
111
            }, 200, $headers);
112
        }
113
114
        return $response->setEtag($eTag);
115
    }
116
}
117