Test Setup Failed
Branch develop (c39a15)
by Nikita
14:07
created

GdaemonCommandsService::__construct()   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 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Gameap\Services;
4
5
use Gameap\Exceptions\GameapException;
6
use Gameap\Models\DedicatedServer;
7
use Knik\Gameap\GdaemonCommands;
8
9
/**
10
 * Class GdaemonCommandsService.
11
 * Class may be used by some modules
12
 *
13
 * @package Gameap\Services
14
 */
15
abstract class GdaemonCommandsService
16
{
17
    /**
18
     * @var GdaemonCommands
19
     */
20
    protected $gdaemonCommands;
21
22
    /**
23
     * GdaemonCommandsService constructor.
24
     *
25
     * @param GdaemonCommands $gdaemonCommands
26
     */
27
    public function __construct(GdaemonCommands $gdaemonCommands)
28
    {
29
        $this->gdaemonCommands = $gdaemonCommands;
30
    }
31
32
    /**
33
     * Setting up gdaemon commands configuration
34
     *
35
     * @param int|DedicatedServer
36
     * @throws GameapException
37
     */
38
    protected function configureGdaemon($ds)
39
    {
40
        if (is_int($ds)) {
41
            $dedicatedServer = DedicatedServer::findOrFail($ds);
42
        } else if ($ds instanceof DedicatedServer) {
43
            $dedicatedServer = $ds;
44
        } else {
45
            throw new GameapException('Invalid type');
46
        }
47
48
        $this->gdaemonCommands->setConfig(
49
            $dedicatedServer->gdaemonSettings()
50
        );
51
    }
52
53
    /**
54
     * @param string $command
55
     * @param array $codes
56
     * @return string
57
     */
58
    protected function replaceShortCodes(string $command, array $codes)
59
    {
60
        foreach ($codes as $code => $value) {
61
            $command = str_replace('{' . $code . '}', $value, $command);
62
        }
63
64
        return $command;
65
    }
66
}