Passed
Push — develop ( 2a02d4...7461cb )
by Nikita
05:45
created

ServerRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 5 1
A store() 0 17 3
A getServersForAuth() 0 6 2
A getServersListForDedicatedServer() 0 5 1
A __construct() 0 4 1
A getServerIdsForDedicatedServer() 0 5 1
A search() 0 8 1
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Models\Server;
6
use Illuminate\Support\Str;
7
use Gameap\Http\Requests\ServerRequest;
8
use Illuminate\Support\Facades\Auth;
9
10
class ServerRepository
11
{
12
    protected $model;
13
14
    protected $gdaemonTaskRepository;
15
16
    public function __construct(Server $server, GdaemonTaskRepository $gdaemonTaskRepository)
17
    {
18
        $this->model = $server;
19
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
20
    }
21
22
    public function getAll($perPage = 20)
23
    {
24
        $servers = Server::orderBy('id')->with('game')->paginate($perPage);
25
26
        return $servers;
27
    }
28
29
    /**
30
     * Store server
31
     * 
32
     * @param array $attributes
33
     */
34
    public function store(array $attributes)
35
    {
36
        $attributes['uuid'] = Str::orderedUuid()->toString();
37
        $attributes['uuid_short'] = Str::substr($attributes['uuid'], 0, 8);
38
39
        $addInstallTask = false;
40
        if (isset($attributes['install'])) {
41
            $attributes['installed'] = ! $attributes['install'];
42
            $addInstallTask = true;
43
44
            unset($attributes['install']);
45
        }
46
47
        $server = Server::create($attributes);
48
49
        if ($addInstallTask) {
50
            $this->gdaemonTaskRepository->addServerUpdate($server);
51
        }
52
    }
53
54
    /**
55
     * Get Servers list for Dedicated server
56
     *
57
     * @param int $dedicatedServerId
58
     */
59
    public function getServersListForDedicatedServer(int $dedicatedServerId)
60
    {
61
        return $this->model->select('*')
62
            ->where('ds_id', '=', $dedicatedServerId)
63
            ->get();
64
    }
65
66
    /**
67
     * Get Servers id list for Dedicated server
68
     *
69
     * @param int $dedicatedServerId
70
     */
71
    public function getServerIdsForDedicatedServer(int $dedicatedServerId)
72
    {
73
        return $this->model->select('id')
74
            ->where('ds_id', '=', $dedicatedServerId)
75
            ->get();
76
    }
77
78
    public function getServersForAuth()
79
    {
80
        if (Auth::user()->can('admin roles & permissions')) {
81
            return $this->getAll();
82
        } else {
83
            return Auth::user()->servers;
0 ignored issues
show
Bug introduced by
Accessing servers on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
84
        }
85
    }
86
87
    public function search($query)
88
    {
89
        return $this->model->select(['id', 'name', 'server_ip', 'server_port', 'game_id', 'game_mod_id'])
90
            ->with(['game' => function($query) {
91
                $query->select('code','name');
92
            }])
93
            ->where('name', 'LIKE', '%' . $query . '%')
94
            ->get();
95
    }
96
}