Passed
Push — develop ( e89a24...51ef1b )
by Nikita
10:04
created

DedicatedServersRepository::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Models\DedicatedServer;
6
use Gameap\Http\Requests\DedicatedServerRequest;
7
use Illuminate\Support\Str;
8
use Illuminate\Support\Facades\Storage;
9
10
class DedicatedServersRepository
11
{
12
    protected $model;
13
14
    public function __construct(DedicatedServer $dedicatedServer)
15
    {
16
        $this->model = $dedicatedServer;
17
    }
18
19
    public function getAll($perPage = 20)
20
    {
21
        return DedicatedServer::orderBy('id')->withCount('servers')->paginate($perPage);
22
    }
23
24
    /**
25
     * @param int $id
26
     * @return array
27
     */
28
    public function getIpList(int $id)
29
    {
30
        return $this->model->select('ip')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->model->sel... '=', $id)->first()->ip returns the type string which is incompatible with the documented return type array.
Loading history...
31
            ->where('id', '=', $id)
32
            ->first()
33
            ->ip;
34
    }
35
36
    /**
37
     * @param array $attributes
38
     */
39
    public function store(array $attributes)
40
    {
41
        $attributes['ip'] = array_filter($attributes['ip'], function($value) {
42
            return !empty($value);
43
        });
44
45
        $attributes['gdaemon_api_key'] = Str::random(64);
46
47
        $attributes['enabled'] = $attributes['enabled'] ?? 1;
48
        $attributes['os'] = $attributes['os'] ?? 'linux';
49
50
        DedicatedServer::create($attributes);
51
    }
52
53
    public function destroy(DedicatedServer $dedicatedServer)
54
    {
55
        if (! Storage::disk('local')->exists('gdaemon_certs/' . $dedicatedServer->gdaemon_server_cert)) {
56
            // TODO: Not working =(
57
            // Storage::disk('local')->delete('gdaemon_certs/' . $dedicatedServer->gdaemon_server_cert);
58
59
            $file = Storage::disk('local')
60
                ->getDriver()
61
                ->getAdapter()
62
                ->applyPathPrefix($dedicatedServer->gdaemon_server_cert);
63
64
            unlink($file);
65
        }
66
67
        $dedicatedServer->delete();
68
    }
69
70
    /**
71
     * @param array $fields
72
     * @param DedicatedServer        $dedicatedServer
73
     */
74
    public function update(DedicatedServer $dedicatedServer, array $attributes)
75
    {
76
        $attributes['ip'] = array_filter($attributes['ip'], function($value) {
77
            return !empty($value);
78
        });
79
80
        $dedicatedServer->update($attributes);
81
    }
82
}