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

ServersController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Gameap\Http\Controllers\Admin;
4
5
use Exception;
6
use Gameap\Exceptions\Repositories\RecordExistExceptions;
7
use Gameap\Http\Controllers\AuthController;
8
use Gameap\Http\Requests\Admin\ServerCreateRequest;
9
use Gameap\Http\Requests\Admin\ServerDestroyRequest;
10
use Gameap\Http\Requests\Admin\ServerUpdateRequest;
11
use Gameap\Models\Game;
12
use Gameap\Models\Server;
13
use Gameap\Models\DedicatedServer;
14
use Gameap\Repositories\GameModRepository;
15
use Gameap\Repositories\ServerRepository;
16
use Gameap\Repositories\GdaemonTaskRepository;
17
use Illuminate\Http\RedirectResponse;
18
use Illuminate\View\View;
19
20
class ServersController extends AuthController
21
{
22
    /**
23
     * The ServerRepository instance.
24
     *
25
     * @var ServerRepository
26
     */
27
    protected $repository;
28
29
    /**
30
     * The GdaemonTaskRepository instance.
31
     *
32
     * @var GdaemonTaskRepository
33
     */
34
    public $gdaemonTaskRepository;
35
36
    /**
37
     * Create a new ServersController instance.
38
     *
39
     * @param ServerRepository $repository
40
     * @param GdaemonTaskRepository $gdaemonTaskRepository
41
     * @param GameModRepository $gameModRepository
42
     */
43 21
    public function __construct(ServerRepository $repository, GdaemonTaskRepository $gdaemonTaskRepository)
44
    {
45 21
        parent::__construct();
46
47 21
        $this->repository = $repository;
48 21
        $this->gdaemonTaskRepository = $gdaemonTaskRepository;
49 21
    }
50
51
    /**
52
     * Display a listing of the resource.
53
     *
54
     * @return View
55
     */
56 3
    public function index()
57
    {
58 3
        return view('admin.servers.list',[
59 3
            'servers' => $this->repository->getAll()
60
        ]);
61
    }
62
63
    /**
64
     * Show the form for creating a new resource.
65
     *
66
     * @return View
67
     */
68 3
    public function create()
69
    {
70 3
        return view('admin.servers.create', [
71 3
            'dedicatedServers' => DedicatedServer::all()->pluck('name', 'id'),
72 3
            'games' => Game::orderBy('name')->get()->pluck('name', 'code')
73
        ]);
74
    }
75
76
    /**
77
     * Store a newly created resource in storage.
78
     *
79
     * @param ServerCreateRequest $request
80
     * @return RedirectResponse
81
     * @throws RecordExistExceptions
82
     */
83 3
    public function store(ServerCreateRequest $request)
84
    {
85 3
        $this->repository->store($request->all());
86
87 3
        return redirect()->route('admin.servers.index')
88 3
            ->with('success', __('servers.create_success_msg'));
89
    }
90
91
    /**
92
     * Show the form for editing the specified resource.
93
     *
94
     * @param Server $server
95
     * @return View
96
     */
97 3
    public function edit(Server $server)
98
    {
99 3
        $dedicatedServers = DedicatedServer::all(['id', 'name'])->pluck('name', 'id');
100 3
        $games = Game::orderBy('name')->get()->pluck('name', 'code');
101 3
        return view('admin.servers.edit', compact('server', 'dedicatedServers', 'games'));
102
    }
103
104
    /**
105
     * Update the specified resource in storage.
106
     *
107
     * @param ServerUpdateRequest $request
108
     * @param Server $server
109
     * @return RedirectResponse
110
     */
111 3
    public function update(ServerUpdateRequest $request, Server $server)
112
    {
113 3
        $this->repository->update($server, $request->all());
114
        
115 3
        return redirect()->route('admin.servers.index')
116 3
            ->with('success', __('servers.update_success_msg'));
117
    }
118
119
    /**
120
     * Remove the specified resource from storage.
121
     * 
122
     * @param Server $server
123
     * @return RedirectResponse
124
     * @throws Exception
125
     */
126 6
    public function destroy(ServerDestroyRequest $request, Server $server)
127
    {
128 6
        if ($request->input('delete_files')) {
129
            try {
130 3
                $this->gdaemonTaskRepository->addServerDelete($server);
131 3
            } catch (RecordExistExceptions $e) {
132
                // Nothing
133
            }
134
135 3
            $server->delete();
136
        } else {
137 3
            $server->forceDelete();
138
        }
139
140 6
        return redirect()->route('admin.servers.index')
141 6
            ->with('success', __('servers.delete_success_msg'));
142
    }
143
}
144