Test Failed
Push — develop ( bbcebd...48e203 )
by Nikita
06:27
created

ServerControlService::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 14
cp 0
rs 9.7666
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Gameap\Services;
4
5
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\EmptyServerStartCommandException;
6
use Gameap\Exceptions\Repositories\GdaemonTaskRepository\InvalidServerStartCommandException;
7
use Gameap\Exceptions\Repositories\RecordExistExceptions;
8
use Gameap\Models\Server;
9
use Gameap\Models\ServerSetting;
10
use Gameap\Repositories\GdaemonTaskRepository;
11
12
class ServerControlService
13
{
14
    /**
15
     * The ServerRepository instance.
16
     *
17
     * @var \Gameap\Repositories\ServerRepository
18
     */
19
    public $repository;
20
21
    /**
22
     * The GdaemonTaskRepository instance.
23
     *
24
     * @var GdaemonTaskRepository
25
     */
26
    public $gdaemonTaskRepository;
27
28
    public function __construct(GdaemonTaskRepository $gdaemonTaskRepository)
29
    {
30
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
31
    }
32
33
    /**
34
     * @param Server $server
35
     * @return int
36
     *
37
     * @throws EmptyServerStartCommandException
38
     * @throws InvalidServerStartCommandException
39
     * @throws RecordExistExceptions
40
     */
41
    public function start(Server $server)
42
    {
43
        $autostartSetting = $server->settings->where('name', 'autostart')->first()
44
            ?? new ServerSetting([
45
                'server_id' => $server->id,
46
                'name'      => 'autostart',
47
                'value'     => true,
48
            ]);
49
50
        if ($autostartSetting->value == true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $autostartSetting->value of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
51
            $autostartCurrentSetting = $server->settings->where('name', 'autostart_current')->first()
52
                ?? new ServerSetting([
53
                    'server_id' => $server->id,
54
                    'name'      => 'autostart_current',
55
                    'value'     => true,
56
                ]);
57
58
            $autostartCurrentSetting->value = true;
59
            $autostartCurrentSetting->save();
60
        }
61
62
        $gdaemonTaskId = $this->gdaemonTaskRepository->addServerStart($server);
63
        return $gdaemonTaskId;
64
    }
65
66
    /**
67
     * @param Server $server
68
     * @return int
69
     *
70
     * @throws EmptyServerStartCommandException
71
     * @throws InvalidServerStartCommandException
72
     * @throws RecordExistExceptions
73
     */
74
    public function stop(Server $server)
75
    {
76
        $autostartCurrentSetting = $server->settings->where('name', 'autostart_current')->first();
77
        $autostartCurrentSetting->value = false;
78
        $autostartCurrentSetting->save();
79
80
        $gdaemonTaskId = $this->gdaemonTaskRepository->addServerStop($server);
81
        return $gdaemonTaskId;
82
    }
83
84
    /**
85
     * @param Server $server
86
     * @return int
87
     *
88
     * @throws EmptyServerStartCommandException
89
     * @throws InvalidServerStartCommandException
90
     * @throws RecordExistExceptions
91
     */
92
    public function restart(Server $server)
93
    {
94
        $autostartSetting = $server->settings->where('name', 'autostart')->first();
95
96
        if ($autostartSetting->value == true) {
97
            $autostartCurrentSetting = $server->settings->where('name', 'autostart_current')->first();
98
            $autostartCurrentSetting->value = true;
99
            $autostartCurrentSetting->save();
100
        }
101
102
        $gdaemonTaskId = $this->gdaemonTaskRepository->addServerRestart($server);
103
        return $gdaemonTaskId;
104
    }
105
106
    /**
107
     * @param Server $server
108
     * @return int
109
     *
110
     * @throws EmptyServerStartCommandException
111
     * @throws InvalidServerStartCommandException
112
     * @throws RecordExistExceptions
113
     */
114
    public function update(Server $server)
115
    {
116
        $gdaemonTaskId = $this->gdaemonTaskRepository->addServerUpdate($server);
117
        return $gdaemonTaskId;
118
    }
119
120
}