Passed
Push — develop ( d2ded6...15ac65 )
by Nikita
19:39
created

DedicatedServersController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Gameap\Http\Controllers\Admin;
4
5
use Gameap\Http\Controllers\AuthController;
6
use Gameap\Http\Requests\Request;
7
use Gameap\Models\ClientCertificate;
8
use Gameap\Models\DedicatedServer;
9
use Gameap\Repositories\DedicatedServersRepository;
10
use Gameap\Http\Requests\Admin\DedicatedServerRequest;
11
use Illuminate\Support\Facades\Cache;
12
use Illuminate\Support\Str;
13
use Illuminate\Support\Facades\Storage;
14
15
class DedicatedServersController extends AuthController
16
{
17
    /**
18
     * The DedicatedServersRepository instance.
19
     *
20
     * @var \Gameap\Repositories\DedicatedServersRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * Create a new DedicatedServersController instance.
26
     *
27
     * @param  \Gameap\Repositories\DedicatedServersRepository $repository
28
     */
29 21
    public function __construct(DedicatedServersRepository $repository)
30
    {
31 21
        parent::__construct();
32
33 21
        $this->repository = $repository;
34 21
    }
35
36
    /**
37
     * Display a listing of the resource.
38
     *
39
     * @return \Illuminate\View\View
40
     */
41 3
    public function index()
42
    {
43 3
        $dedicatedServers = $this->repository->getAll();
44
45 3
        return view('admin.dedicated_servers.list', [
46 3
            'dedicatedServers' => $dedicatedServers
47
        ]);
48
    }
49
50
    /**
51
     * Show the form for creating a new resource.
52
     *
53
     * @return \Illuminate\View\View
54
     */
55 3
    public function create()
56
    {
57
        // Add auto setup token
58 3
        $autoSetupToken = Str::random(24);
59 3
        Cache::put('gdaemonAutoSetupToken', $autoSetupToken, 300);
60
61 3
        $clientCertificates = ClientCertificate::all(['id', 'fingerprint'])->pluck('fingerprint', 'id');
62 3
        return view('admin.dedicated_servers.create', compact('clientCertificates', 'autoSetupToken'));
63
    }
64
65
    /**
66
     * Store a newly created resource in storage.
67
     *
68
     * @param  \Gameap\Http\Requests\Admin\DedicatedServerRequest  $request
69
     * @return \Illuminate\Http\RedirectResponse
70
     */
71 3
    public function store(DedicatedServerRequest $request)
72
    {
73 3
        $attributes = $request->all();
74
75 3
        if ($request->hasFile('gdaemon_server_cert')) {
76
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
77
                'certs/server', 'local'
78
            );
79
        }
80
81 3
        $this->repository->store($attributes);
82
83 3
        return redirect()->route('admin.dedicated_servers.index')
84 3
            ->with('success', __('dedicated_servers.create_success_msg'));
85
    }
86
87
    /**
88
     * Display the specified resource.
89
     *
90
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
91
     * @return \Illuminate\View\View
92
     */
93 3
    public function show(DedicatedServer $dedicatedServer)
94
    {
95 3
        return view('admin.dedicated_servers.view', compact('dedicatedServer'));
96
    }
97
98
    /**
99
     * Show the form for editing the specified resource.
100
     *
101
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
102
     * @return \Illuminate\View\View
103
     */
104 3
    public function edit(DedicatedServer $dedicatedServer)
105
    {
106 3
        $clientCertificates = ClientCertificate::all(['id', 'fingerprint'])->pluck('fingerprint', 'id');
107 3
        return view('admin.dedicated_servers.edit', compact('dedicatedServer', 'clientCertificates'));
108
    }
109
110
    /**
111
     * Update the specified resource in storage.
112
     *
113
     * @param  \Gameap\Http\Requests\Admin\DedicatedServerRequest  $request
114
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
115
     * @return \Illuminate\Http\RedirectResponse
116
     */
117 3
    public function update(DedicatedServerRequest $request, DedicatedServer $dedicatedServer)
118
    {
119 3
        $attributes = $request->all();
120
        
121 3
        if ($request->hasFile('gdaemon_server_cert')) {
122
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
123
                'certs/server', 'local'
124
            );
125
126
            $certificateFile = Storage::disk('local')
127
                ->getDriver()
128
                ->getAdapter()
129
                ->applyPathPrefix($dedicatedServer->gdaemon_server_cert);
130
            
131
            if (file_exists($certificateFile)) {
132
                unlink($certificateFile);
133
            }
134
        }
135
        
136 3
        $this->repository->update($dedicatedServer, $attributes);
137
138 3
        return redirect()->route('admin.dedicated_servers.index')
139 3
            ->with('success',  __('dedicated_servers.update_success_msg'));
140
    }
141
142
    /**
143
     * Remove the specified resource from storage.
144
     *
145
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
146
     * @return \Illuminate\Http\RedirectResponse
147
     */
148 3
    public function destroy(DedicatedServer $dedicatedServer)
149
    {
150 3
        $this->repository->destroy($dedicatedServer);
151
152 3
        return redirect()->route('admin.dedicated_servers.index')
153 3
            ->with('success',  __('dedicated_servers.delete_success_msg'));
154
    }
155
}
156