Test Setup Failed
Push — develop ( 6f7221...94b6b4 )
by Nikita
06:49
created

DedicatedServersRepository::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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