ServersSettingsController::get()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 58
rs 8.1155
cc 8
nc 12
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Gameap\Helpers\PermissionHelper;
6
use Gameap\Http\Controllers\AuthController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gameap\Http\Controllers\API\AuthController. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Gameap\Http\Requests\API\ServerSettingSaveRequest;
8
use Gameap\Models\Server;
9
use Gameap\Models\User;
10
use Gameap\Repositories\ServerRepository;
11
use Illuminate\Contracts\Auth\Factory as AuthFactory;
12
use Symfony\Component\Serializer\SerializerInterface;
13
14
class ServersSettingsController extends AuthController
15
{
16
    /**
17
     * The ServerRepository instance.
18
     *
19
     * @var \Gameap\Repositories\ServerRepository
20
     */
21
    public $repository;
22
23
    /** @var SerializerInterface */
24
    protected $serializer;
25
26
    /** @var AuthFactory */
27
    protected $authFactory;
28
29
    /**
30
     * ServersController constructor.
31
     * @param ServerRepository $repository
32
     */
33
    public function __construct(
34
        ServerRepository $repository,
35
        SerializerInterface $serializer,
36
        AuthFactory $authFactory
37
    ) {
38
        parent::__construct();
39
40
        $this->repository            = $repository;
41
        $this->serializer            = $serializer;
42
        $this->authFactory           = $authFactory;
43
    }
44
45
    public function get(Server $server)
46
    {
47
        /** @var User $currentUser */
48
        $currentUser = $this->authFactory->guard()->user();
49
50
        $this->authorize('server-control', $server);
51
        $this->authorize('server-settings', $server);
52
53
        $isAdmin = $currentUser->can(PermissionHelper::ADMIN_PERMISSIONS);
54
55
        $settings = [
56
            $server::AUTOSTART_SETTING_KEY => [
57
                'name' => $server::AUTOSTART_SETTING_KEY,
58
                'value' => $server->getSetting($server::AUTOSTART_SETTING_KEY)->value,
59
                'type' => 'bool',
60
                'label' => __('servers.autostart_setting'),
61
            ],
62
            $server::UPDATE_BEFORE_START_SETTING_KEY => [
63
                'name' => $server::UPDATE_BEFORE_START_SETTING_KEY,
64
                'value' => $server->getSetting($server::UPDATE_BEFORE_START_SETTING_KEY)->value,
65
                'type' => 'bool',
66
                'label' => __('servers.update_before_start_setting'),
67
            ],
68
        ];
69
70
        foreach($server->gameMod->vars as $var) {
0 ignored issues
show
Bug introduced by
The expression $server->gameMod->vars of type string is not traversable.
Loading history...
71
            if (!empty($var['admin_var']) && !$isAdmin) {
72
                continue;
73
            }
74
75
            $settings[$var['var']] = [
76
                'name' => $var['var'],
77
                'value' => $var['default'],
78
                'type' => $var['type'] ?? 'string',
79
                'label' => $var['info'] ?? $var['var'],
80
            ];
81
        }
82
83
        foreach($server->settings as $setting) {
84
            if (!isset($settings[$setting->name])) {
85
                continue;
86
            }
87
88
            if (!empty($settings[$setting->name]['admin_var']) && !$isAdmin) {
89
                continue;
90
            }
91
92
            $settings[$setting->name] = [
93
                'name' => $setting->name,
94
                'value' => $setting->value,
95
                'label' => $settings[$setting->name]['label'] ?? $setting->name,
96
                'type' => $settings[$setting->name]['type'] ?? 'string',
97
            ];
98
        }
99
100
        unset($settings[$server::AUTOSTART_CURRENT_SETTING_KEY]);
101
102
        return array_values($settings);
103
    }
104
105
    public function save(Server $server, ServerSettingSaveRequest $request)
106
    {
107
        /** @var User $currentUser */
108
        $currentUser = $this->authFactory->guard()->user();
109
110
        $this->authorize('server-control', $server);
111
        $this->authorize('server-settings', $server);
112
113
        $isAdmin = $currentUser->can(PermissionHelper::ADMIN_PERMISSIONS);
114
115
        $settingsInput = $request->all();
116
117
        $settings = [
118
            $server::AUTOSTART_SETTING_KEY => [
119
                'name' => $server::AUTOSTART_SETTING_KEY,
120
                'value' => $server->getSetting($server::AUTOSTART_SETTING_KEY)->value,
121
                'type' => 'bool',
122
                'label' => __('servers.autostart_setting'),
123
            ],
124
            $server::UPDATE_BEFORE_START_SETTING_KEY => [
125
                'name' => $server::UPDATE_BEFORE_START_SETTING_KEY,
126
                'value' => $server->getSetting($server::UPDATE_BEFORE_START_SETTING_KEY)->value,
127
                'type' => 'bool',
128
                'label' => __('servers.update_before_start_setting'),
129
            ],
130
        ];
131
132
        foreach($server->gameMod->vars as $var) {
0 ignored issues
show
Bug introduced by
The expression $server->gameMod->vars of type string is not traversable.
Loading history...
133
            if (!empty($var['admin_var']) && !$isAdmin) {
134
                continue;
135
            }
136
137
            $settings[$var['var']] = [
138
                'name' => $var['var'],
139
                'value' => $var['default'],
140
                'type' => $var['type'] ?? 'string',
141
                'label' => $var['info'] ?? $var['var'],
142
            ];
143
        }
144
145
        /** @var ServerSettingSaveRequest[][] $settingsInputMap */
146
        $settingsInputMap = [];
147
        foreach($settingsInput as $setting) {
148
            $settingsInputMap[$setting['name']] = $setting['value'];
149
        }
150
151
        foreach($settings as $setting) {
152
            if (!isset($settingsInputMap[$setting['name']])) {
153
                continue;
154
            }
155
156
            if (!empty($setting['admin_var']) && !$isAdmin) {
157
                continue;
158
            }
159
160
            $value = $server->getSetting($setting['name']);
161
            $value->value = $settingsInputMap[$setting['name']];
162
            $value->save();
163
        }
164
165
        $this->repository->save($server);
166
    }
167
}