Passed
Push — develop ( 8faf36...a6a6d6 )
by Nikita
12:20
created

DedicatedServersRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Models\DedicatedServer;
6
use Gameap\Models\ClientCertificate;
7
use Gameap\Http\Requests\DedicatedServerRequest;
0 ignored issues
show
Bug introduced by
The type Gameap\Http\Requests\DedicatedServerRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Gameap\Repositories\ClientCertificateRepository;
9
use Gameap\Services\CertificateService;
10
use Illuminate\Support\Str;
11
use Illuminate\Support\Facades\Storage;
12
13
class DedicatedServersRepository extends Repository
14
{
15
    /**
16
     * @var \Gameap\Repositories\ClientCertificateRepository
17
     */
18
    protected $clientCertificateRepository;
19
20
    /**
21
     * DedicatedServersRepository constructor.
22
     * @param DedicatedServer $dedicatedServer
23
     * @param \Gameap\Repositories\ClientCertificateRepository $clientCertificateRepository
24
     */
25
    public function __construct(
26
        DedicatedServer $dedicatedServer,
27
        ClientCertificateRepository $clientCertificateRepository
28
    ) {
29
        $this->model = $dedicatedServer;
30
        $this->clientCertificateRepository = $clientCertificateRepository;
31
    }
32
33
    /**
34
     * @param int $perPage
35
     * @return mixed
36
     */
37
    public function getAll($perPage = 20)
38
    {
39
        return DedicatedServer::orderBy('id')->withCount('servers')->paginate($perPage);
40
    }
41
42
    /**
43
     * @param int $id
44
     * @return array
45
     */
46
    public function getIpList(int $id)
47
    {
48
        return $this->model->select('ip')
49
            ->where('id', '=', $id)
50
            ->first()
51
            ->ip;
52
    }
53
54
    /**
55
     * Get all busy ports for dedicated servers. Group by ip
56
     *
57
     * @param int $id
58
     * @return \Illuminate\Support\Collection|array
59
     */
60
    public function getBusyPorts(int $id)
61
    {
62
        /** @var DedicatedServer $dedicatedServer */
63
        $dedicatedServer = $this->model->select('id')->where('id', '=', $id)->first();
64
        $result = collect();
65
66
        foreach ($dedicatedServer->servers as $server) {
67
            if (!$result->has($server->server_ip)) {
68
                $result->put($server->server_ip, collect([
69
                    $server->server_port,
70
                    $server->query_port,
71
                    $server->rcon_port
72
                ]));
73
            } else {
74
                $result[$server->server_ip]
75
                    ->push($server->server_port)
76
                    ->push($server->query_port)
77
                    ->push($server->rcon_port);
78
            }
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * @param array $attributes
86
     * @return DedicatedServer
87
     */
88
    public function store(array $attributes)
89
    {
90
        $attributes['ip'] = array_filter($attributes['ip'], function($value) {
91
            return !empty($value);
92
        });
93
94
        if (empty($attributes['client_certificate_id'])) {
95
            $clientCertificate = $this->clientCertificateRepository->getFirstOrGenerate();
96
            $attributes['client_certificate_id'] = $clientCertificate->id;
97
        }
98
99
        $attributes['gdaemon_api_key'] = Str::random(64);
100
101
        $attributes['enabled'] = $attributes['enabled'] ?? 1;
102
        $attributes['os'] = $attributes['os'] ?? 'linux';
103
104
        return DedicatedServer::create($attributes);
105
    }
106
107
    /**
108
     * @param DedicatedServer $dedicatedServer
109
     * @throws \Exception
110
     */
111
    public function destroy(DedicatedServer $dedicatedServer)
112
    {
113
        if ($dedicatedServer->gdaemon_server_cert != CertificateService::ROOT_CA_CERT &&
114
            Storage::disk('local')->exists($dedicatedServer->gdaemon_server_cert)
115
        ) {
116
            // TODO: Not working =(
117
            // Storage::disk('local')->delete($dedicatedServer->gdaemon_server_cert);
118
119
            $certificateFile = Storage::disk('local')
120
                ->getDriver()
121
                ->getAdapter()
122
                ->applyPathPrefix($dedicatedServer->gdaemon_server_cert);
123
124
            if (file_exists($certificateFile)) {
125
                unlink($certificateFile);
126
            }
127
        }
128
129
        $dedicatedServer->delete();
130
    }
131
132
    /**
133
     * @param array $fields
134
     * @param DedicatedServer        $dedicatedServer
135
     */
136
    public function update(DedicatedServer $dedicatedServer, array $attributes)
137
    {
138
        $attributes['ip'] = array_filter($attributes['ip'], function($value) {
139
            return !empty($value);
140
        });
141
142
        $dedicatedServer->update($attributes);
143
    }
144
}