Test Setup Failed
Push — develop ( 0938c3...b132ab )
by Nikita
06:18
created

DedicatedServersController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Gameap\Http\Controllers\Admin;
4
5
use Gameap\Helpers\OsHelper;
6
use Gameap\Http\Controllers\AuthController;
7
use Gameap\Http\Requests\Admin\DedicatedServerRequest;
8
use Gameap\Models\ClientCertificate;
9
use Gameap\Models\DedicatedServer;
10
use Gameap\Repositories\DedicatedServersRepository;
11
use Gameap\Services\Daemon\CertificateService;
12
use Gameap\Services\Daemon\DebugService;
13
use Illuminate\Support\Facades\Cache;
14
use Illuminate\Support\Facades\Storage;
15
use Illuminate\Support\Str;
16
use Knik\Gameap\GdaemonStatus;
17
use RuntimeException;
18
use ZipArchive;
19
20
class DedicatedServersController extends AuthController
21
{
22
    /**
23
     * The DedicatedServersRepository instance.
24
     *
25
     * @var \Gameap\Repositories\DedicatedServersRepository
26
     */
27
    protected $repository;
28
29
    /** @var DebugService */
30
    protected $debugService;
31 30
32
    public function __construct(
33 30
        DedicatedServersRepository $repository,
34
        DebugService $downloadDebugService
35 30
    ) {
36 30
        parent::__construct();
37
38
        $this->repository           = $repository;
39
        $this->debugService = $downloadDebugService;
40
    }
41
42
    /**
43 6
     * Display a listing of the resource.
44
     *
45 6
     * @return \Illuminate\View\View
46
     */
47 6
    public function index()
48 6
    {
49
        $dedicatedServers = $this->repository->getAll();
50
51
        return view('admin.dedicated_servers.list', [
52
            'dedicatedServers' => $dedicatedServers,
53
        ]);
54
    }
55
56
    /**
57 3
     * Show the form for creating a new resource.
58
     *
59
     * @return \Illuminate\View\View
60 3
     */
61
    public function create()
62 3
    {
63 3
        // Add auto setup token
64 3
        $autoSetupToken = env('DAEMON_SETUP_TOKEN');
65
66
        if (empty($autoSetupToken)) {
67 3
            $autoSetupToken = Str::random(24);
68 3
            Cache::put('gdaemonAutoSetupToken', $autoSetupToken, 300);
69
        }
70
71
        $clientCertificates = ClientCertificate::all(['id', 'fingerprint'])->pluck('fingerprint', 'id');
72
        return view('admin.dedicated_servers.create', compact('clientCertificates', 'autoSetupToken'));
73
    }
74
75
    /**
76
     * Store a newly created resource in storage.
77 3
     *
78
     * @param  \Gameap\Http\Requests\Admin\DedicatedServerRequest  $request
79 3
     * @return \Illuminate\Http\RedirectResponse
80
     */
81 3
    public function store(DedicatedServerRequest $request)
82
    {
83
        $attributes = $request->all();
84
85
        if ($request->hasFile('gdaemon_server_cert')) {
86
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
87 3
                'certs/server',
88
                'local'
89 3
            );
90 3
        }
91
92
        $this->repository->store($attributes);
93
94
        return redirect()->route('admin.dedicated_servers.index')
95
            ->with('success', __('dedicated_servers.create_success_msg'));
96
    }
97
98
    /**
99
     * Display the specified resource.
100 6
     *
101
     * @param \Gameap\Models\DedicatedServer $dedicatedServer
102 6
     * @param GdaemonStatus $gdaemonStatus
103
     * @return \Illuminate\View\View
104
     */
105 6
    public function show(DedicatedServer $dedicatedServer, GdaemonStatus $gdaemonStatus)
106 3
    {
107 3
        $gdaemonStatus->setConfig($dedicatedServer->gdaemonSettings());
108 3
109 3
        try {
110
            $gdaemonVersion = $gdaemonStatus->version();
111
            $baseInfo       = $gdaemonStatus->infoBase();
112 6
        } catch (RuntimeException $e) {
113 6
            $gdaemonVersion = [];
114 6
            $baseInfo       = [];
115 6
        }
116 6
117
        return view(
118
            'admin.dedicated_servers.view',
119
            compact(
120
                'dedicatedServer',
121
                'gdaemonVersion',
122
                'baseInfo'
123
            )
124
        );
125
    }
126
127 6
    /**
128
     * Show the form for editing the specified resource.
129 6
     *
130 6
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
131
     * @return \Illuminate\View\View
132
     */
133
    public function edit(DedicatedServer $dedicatedServer)
134
    {
135
        $clientCertificates = ClientCertificate::all(['id', 'fingerprint'])->pluck('fingerprint', 'id');
136
        return view('admin.dedicated_servers.edit', compact('dedicatedServer', 'clientCertificates'));
137
    }
138
139
    /**
140 3
     * Update the specified resource in storage.
141
     *
142 3
     * @param  \Gameap\Http\Requests\Admin\DedicatedServerRequest  $request
143
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
144 3
     * @return \Illuminate\Http\RedirectResponse
145
     */
146
    public function update(DedicatedServerRequest $request, DedicatedServer $dedicatedServer)
147
    {
148
        $attributes = $request->all();
149
        
150
        if ($request->hasFile('gdaemon_server_cert')) {
151
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
152
                'certs/server',
153
                'local'
154
            );
155
156
            $certificateFile = Storage::disk('local')
157
                ->getDriver()
158
                ->getAdapter()
159 3
                ->applyPathPrefix($dedicatedServer->gdaemon_server_cert);
160
            
161 3
            if (file_exists($certificateFile)) {
162 3
                unlink($certificateFile);
163
            }
164
        }
165
        
166
        $this->repository->update($dedicatedServer, $attributes);
167
168
        return redirect()->route('admin.dedicated_servers.index')
169
            ->with('success', __('dedicated_servers.update_success_msg'));
170
    }
171 3
172
    /**
173 3
     * Remove the specified resource from storage.
174
     *
175 3
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
176 3
     * @return \Illuminate\Http\RedirectResponse
177
     */
178
    public function destroy(DedicatedServer $dedicatedServer)
179
    {
180
        $this->repository->destroy($dedicatedServer);
181
182
        return redirect()->route('admin.dedicated_servers.index')
183
            ->with('success', __('dedicated_servers.delete_success_msg'));
184
    }
185
186
    public function logsZip(DedicatedServer $dedicatedServer)
187
    {
188
        try {
189
            $zipPath = $this->debugService->downloadLogs($dedicatedServer);
190
        } catch (RuntimeException $exception) {
191
            return redirect()->route('admin.dedicated_servers.show', [$dedicatedServer->id])
192
                ->with('error', $exception->getMessage());
193
        }
194
195
        return response()->download($zipPath, "logs.zip");
196
    }
197
198
    public function certificatesZip(DedicatedServer $dedicatedServer)
0 ignored issues
show
Unused Code introduced by
The parameter $dedicatedServer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

198
    public function certificatesZip(/** @scrutinizer ignore-unused */ DedicatedServer $dedicatedServer)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
199
    {
200
        $key                     = CertificateService::generateKey();
201
        $csr                     = CertificateService::generateCsr($key);
202
        $serverSignedCertificate = CertificateService::signCsr($csr);
203
204
        $zipFilePath = OsHelper::tempFile();
205
        $zip = new ZipArchive();
206
        $zip->open($zipFilePath, ZipArchive::CREATE);
207
        $zip->addFromString("server.key", $key);
208
        $zip->addFromString("server.crt", $serverSignedCertificate);
209
        $zip->addFromString("ca.crt", CertificateService::getRootCert());
210
        $zip->addFromString(
211
            "README.md",
212
            "* Move this files to certs directory (For linux: /etc/gameap-daemon/certs/)\n" .
213
            "* Edit gameap-daemon configuration, set `ca_certificate_file`, `certificate_chain_file` and `private_key_file`"
214
        );
215
        $zip->close();
216
217
        return response()->download($zipFilePath, "certificates.zip");
218
    }
219
}
220