Completed
Pull Request — master (#36)
by Owen
02:07 queued 30s
created

TerminalController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 57.5%

Importance

Changes 0
Metric Value
dl 0
loc 109
ccs 23
cts 40
cp 0.575
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 16 3
A endpoint() 0 15 2
A media() 0 27 4
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)
0 ignored issues
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...
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
        }
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
        ]));
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(
73 1
            $this->request->get('command'),
74 1
            $this->request->get('parameters', [])
75
        );
76
77 1
        $key = $error === 0 ? 'result' : 'error';
78
79 1
        return $this->responseFactory->json([
80 1
            'jsonrpc' => $this->request->get('jsonrpc'),
81 1
            'id' => $this->request->get('id'),
82 1
            $key => $kernel->output(),
83
        ]);
84
    }
85
86
    /**
87
     * media.
88
     *
89
     * @param \Illuminate\Filesystem\Filesystem $files
90
     * @param string $file
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function media(Filesystem $files, $file)
94
    {
95
        $filename = __DIR__.'/../../../public/'.$file;
96
        $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript';
97
        $lastModified = $files->lastModified($filename);
98
        $eTag = sha1_file($filename);
99
        $headers = [
100
            'content-type' => $mimeType,
101
            'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT',
102
        ];
103
104
        if (@strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified ||
105
            trim($this->request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag
106
        ) {
107
            $response = $this->responseFactory->make(null, 304, $headers);
108
        } else {
109
            $response = $this->responseFactory->stream(function () use ($filename) {
110
                $out = fopen('php://output', 'wb');
111
                $file = fopen($filename, 'rb');
112
                stream_copy_to_stream($file, $out, filesize($filename));
113
                fclose($out);
114
                fclose($file);
115
            }, 200, $headers);
116
        }
117
118
        return $response->setEtag($eTag);
119
    }
120
}
121