Passed
Push — develop ( 69e663...ab572c )
by Nikita
05:33
created

SetupController::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Http\Controllers\GdaemonAPI;
4
5
use Gameap\Repositories\DedicatedServersRepository;
6
use Gameap\Models\DedicatedServer;
7
use Illuminate\Routing\Controller as BaseController;
8
use Illuminate\Support\Facades\Cache;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
use Illuminate\Support\Str;
11
use Gameap\Http\Requests\GdaemonAPI\DedicatedServerRequest;
12
use Illuminate\Http\Request;
13
use Storage;
14
15
class SetupController extends BaseController
16
{
17
    /**
18
     * The DedicatedServersRepository instance.
19
     *
20
     * @var \Gameap\Repositories\DedicatedServersRepository
21
     */
22
    public $repository;
23
24
    /**
25
     * DedicatedServersController constructor.
26
     * @param DedicatedServersRepository $repository
27
     */
28
    public function __construct(DedicatedServersRepository $repository)
29
    {
30
        $this->repository = $repository;
31
    }
32
33
    /**
34
     * Return path to daemon script setup
35
     */
36
    public function setup(string $token)
0 ignored issues
show
Unused Code introduced by
The parameter $token 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

36
    public function setup(/** @scrutinizer ignore-unused */ string $token)

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...
37
    {
38
        app('debugbar')->disable();
0 ignored issues
show
Bug introduced by
The method disable() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

38
        app('debugbar')->/** @scrutinizer ignore-call */ disable();

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...
39
40
        Cache::forget('gdaemonAutoSetupToken');
41
42
        $gdaemonCreateToken = Str::random(24);
43
        Cache::put('gdaemonCreateToken', $gdaemonCreateToken, 30);
44
45
        return "export createToken={$gdaemonCreateToken};
46
            export panelHost=" . url('/') . ";
47
            curl -sL https://raw.githubusercontent.com/gameap/auto-install-scripts/master/debian/install-gdaemon-en.sh | bash --";
48
    }
49
50
    /**
51
     * Creating a new Dedicated server. Uploading certificate
52
     */
53
    public function create(string $token, DedicatedServerRequest $request)
54
    {
55
        app('debugbar')->disable();
56
        $gdaemonCreateToken = Cache::get('gdaemonCreateToken');
0 ignored issues
show
Unused Code introduced by
The assignment to $gdaemonCreateToken is dead and can be removed.
Loading history...
57
        Cache::forget('gdaemonCreateToken');
58
59
        $attributes = $request->all();
60
61
        if ($request->hasFile('gdaemon_server_cert')) {
62
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
63
                'gdaemon_certs', 'local'
64
            );
65
        } else {
66
            return "Error Empty GDdaemon server certificate";
67
        }
68
69
        $dedicatedServer = $this->repository->store($attributes);
70
71
        $certificate = Storage::disk('local')->get($dedicatedServer->clientCertificate->certificate);
72
73
        return "Success {$dedicatedServer->id} {$dedicatedServer->gdaemon_api_key}\n{$certificate}";
74
    }
75
}