Skip to content

Commit 5c3dc60

Browse files
committed
Addition of repository to ease testing and maintainability
1 parent 2f4ec64 commit 5c3dc60

File tree

22 files changed

+607
-843
lines changed

22 files changed

+607
-843
lines changed

app/Contracts/Repositories/RepositoryInterface.php

Lines changed: 0 additions & 164 deletions
This file was deleted.

app/Contracts/Repositories/UserInterface.php renamed to app/Contracts/Repository/Attributes/SearchableInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
* SOFTWARE.
2323
*/
2424

25-
namespace Pterodactyl\Contracts\Repositories;
25+
namespace Pterodactyl\Contracts\Repository\Attributes;
2626

27-
interface UserInterface extends RepositoryInterface, SearchableRepositoryInterface
27+
interface SearchableInterface
2828
{
29-
//
29+
public function search($term);
3030
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Contracts\Repository;
26+
27+
interface RepositoryInterface
28+
{
29+
public function model();
30+
31+
public function getModel();
32+
33+
public function getBuilder();
34+
35+
public function getColumns();
36+
37+
public function withColumns($columns = ['*']);
38+
39+
public function create($fields);
40+
41+
public function delete($id);
42+
43+
public function find($id);
44+
45+
public function findWhere($fields);
46+
47+
public function update($id, $fields);
48+
49+
public function massUpdate($fields);
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Contracts\Repository;
26+
27+
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
28+
29+
interface UserRepositoryInterface extends RepositoryInterface, SearchableInterface
30+
{
31+
public function getAllUsersWithCounts();
32+
33+
public function deleteIfNoServers($id);
34+
}

app/Contracts/Repositories/SearchableRepositoryInterface.php renamed to app/Exceptions/Repository/RecordNotFoundException.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
* SOFTWARE.
2323
*/
2424

25-
namespace Pterodactyl\Contracts\Repositories;
25+
namespace Pterodactyl\Exceptions\Repository;
2626

27-
interface SearchableRepositoryInterface extends RepositoryInterface
27+
use Illuminate\Database\Eloquent\ModelNotFoundException;
28+
29+
class RecordNotFoundException extends ModelNotFoundException
2830
{
29-
/**
30-
* Pass parameters to search trait on model.
31-
*
32-
* @param string $term
33-
* @return mixed
34-
*/
35-
public function search($term);
31+
//
3632
}

app/Http/Controllers/Admin/UserController.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

2727
use Illuminate\Http\Request;
28+
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2829
use Pterodactyl\Models\User;
2930
use Prologue\Alerts\AlertsMessageBag;
3031
use Pterodactyl\Services\UserService;
@@ -49,18 +50,29 @@ class UserController extends Controller
4950
*/
5051
protected $model;
5152

53+
/**
54+
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
55+
*/
56+
protected $repository;
57+
5258
/**
5359
* UserController constructor.
5460
*
55-
* @param \Prologue\Alerts\AlertsMessageBag $alert
56-
* @param \Pterodactyl\Services\UserService $service
57-
* @param \Pterodactyl\Models\User $model
61+
* @param \Prologue\Alerts\AlertsMessageBag $alert
62+
* @param \Pterodactyl\Services\UserService $service
63+
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
64+
* @param \Pterodactyl\Models\User $model
5865
*/
59-
public function __construct(AlertsMessageBag $alert, UserService $service, User $model)
60-
{
66+
public function __construct(
67+
AlertsMessageBag $alert,
68+
UserService $service,
69+
UserRepositoryInterface $repository,
70+
User $model
71+
) {
6172
$this->alert = $alert;
6273
$this->service = $service;
6374
$this->model = $model;
75+
$this->repository = $repository;
6476
}
6577

6678
/**
@@ -71,14 +83,10 @@ public function __construct(AlertsMessageBag $alert, UserService $service, User
7183
*/
7284
public function index(Request $request)
7385
{
74-
$users = $this->model->newQuery()->withCount('servers', 'subuserOf');
75-
76-
if (! is_null($request->input('query'))) {
77-
$users->search($request->input('query'));
78-
}
86+
$users = $this->repository->search($request->input('query'))->getAllUsersWithCounts();
7987

8088
return view('admin.users.index', [
81-
'users' => $users->paginate(config('pterodactyl.paginate.admin.users')),
89+
'users' => $users,
8290
]);
8391
}
8492

@@ -122,7 +130,7 @@ public function delete(Request $request, User $user)
122130
}
123131

124132
try {
125-
$this->service->delete($user->id);
133+
$this->repository->deleteIfNoServers($user->id);
126134

127135
return redirect()->route('admin.users');
128136
} catch (DisplayException $ex) {
@@ -144,6 +152,7 @@ public function delete(Request $request, User $user)
144152
public function store(UserFormRequest $request)
145153
{
146154
$user = $this->service->create($request->normalize());
155+
147156
$this->alert->success('Account has been successfully created.')->flash();
148157

149158
return redirect()->route('admin.users.view', $user->id);

app/Http/Requests/Admin/AdminFormRequest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
abstract class AdminFormRequest extends FormRequest
3030
{
31+
abstract public function rules();
32+
3133
/**
3234
* Determine if the user is an admin and has permission to access this
3335
* form controller in the first place.
@@ -47,12 +49,14 @@ public function authorize()
4749
* Return only the fields that we are interested in from the request.
4850
* This will include empty fields as a null value.
4951
*
52+
* @param array $only
5053
* @return array
5154
*/
52-
public function normalize()
55+
public function normalize($only = [])
5356
{
54-
return $this->only(
55-
array_keys($this->rules())
57+
return array_merge(
58+
$this->only($only),
59+
$this->intersect(array_keys($this->rules()))
5660
);
5761
}
5862
}

app/Http/Requests/Admin/UserFormRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function rules()
4040
return User::getCreateRules();
4141
}
4242

43-
public function normalize()
43+
public function normalize($only = [])
4444
{
4545
if ($this->method === 'PATCH') {
4646
return array_merge(

0 commit comments

Comments
 (0)