SetupController::create()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 15
c 1
b 1
f 0
dl 0
loc 27
ccs 0
cts 11
cp 0
rs 9.7666
cc 4
nc 6
nop 2
crap 20
1
<?php
2
3
namespace Gameap\Http\Controllers\GdaemonAPI;
4
5
use Gameap\Exceptions\GameapException;
6
use Gameap\Repositories\NodeRepository;
7
use Gameap\Services\Daemon\CertificateService;
8
use Illuminate\Contracts\Filesystem\FileNotFoundException;
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\Controller as BaseController;
11
use Illuminate\Support\Facades\Cache;
12
use Illuminate\Support\Facades\Storage;
13
use Illuminate\Support\Str;
14
15
class SetupController extends BaseController
16
{
17
    private const CREATE_TOKEN_LENGTH = 24;
18
19
    private const CREATE_TOKEN_TTL_IN_SECONDS = 3600;
20
21
    /**
22
     * The DedicatedServersRepository instance.
23
     *
24
     * @var \Gameap\Repositories\NodeRepository
25
     */
26
    public $repository;
27
28
    /**
29
     * DedicatedServersController constructor.
30
     * @param NodeRepository $repository
31
     */
32
    public function __construct(NodeRepository $repository)
33
    {
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * Return path to daemon script setup
39
     */
40
    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

40
    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...
41
    {
42
        if (app()->has('debugbar')) {
43
            app('debugbar')->disable();
0 ignored issues
show
Bug introduced by
The method disable() does not exist on Illuminate\Contracts\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

43
            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...
44
        }
45
46
        Cache::forget('gdaemonAutoSetupToken');
47
48
        $gdaemonCreateToken = Str::random(self::CREATE_TOKEN_LENGTH);
49
        Cache::put('gdaemonAutoCreateToken', $gdaemonCreateToken, self::CREATE_TOKEN_TTL_IN_SECONDS);
50
51
        return "export createToken={$gdaemonCreateToken};
52
            export panelHost=" . url('/') . ';
53
            curl -sL https://raw.githubusercontent.com/gameap/auto-install-scripts/master/install-gdaemon.sh | bash --';
54
    }
55
56
    /**
57
     * Creating a new Dedicated server. Uploading certificate
58
     *
59
     * Gets dedicated server attributes and server certificate
60
     * Return signed client certificate and signed server certificate
61
     *
62
     * @param string $token
63
     * @param Request $request
64
     * @return string
65
     *
66
     * @throws GameapException
67
     * @throws FileNotFoundException
68
     */
69
    public function create(string $token, Request $request)
70
    {
71
        if (app()->has('debugbar')) {
72
            app('debugbar')->disable();
73
        }
74
75
        $attributes = $request->all();
76
77
        if ($request->hasFile('gdaemon_server_cert')) {
78
            $csr                     = $request->file('gdaemon_server_cert')->get();
79
            $serverSignedCertificate = CertificateService::signCsr($csr);
80
        } else {
81
            return 'Error Empty GDdaemon server certificate';
82
        }
83
84
        if (empty($attributes['location'])) {
85
            $attributes['location'] = 'Unknown';
86
        }
87
88
        $attributes['gdaemon_server_cert'] = CertificateService::ROOT_CA_CERT;
89
90
        $dedicatedServer = $this->repository->store($attributes);
91
        $certificate     = CertificateService::getRootCert();
92
93
        Cache::forget('gdaemonCreateToken');
94
95
        return "Success {$dedicatedServer->id} {$dedicatedServer->gdaemon_api_key}\n{$certificate}\n\n{$serverSignedCertificate}";
96
    }
97
}
98