Skip to content

Commit f0e18ba

Browse files
committed
Code cleanup
1 parent a2201aa commit f0e18ba

File tree

4 files changed

+19
-87
lines changed

4 files changed

+19
-87
lines changed

app/Contracts/Repository/AllocationRepositoryInterface.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,9 @@
33
namespace Pterodactyl\Contracts\Repository;
44

55
use Illuminate\Support\Collection;
6-
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
76

87
interface AllocationRepositoryInterface extends RepositoryInterface
98
{
10-
/**
11-
* Set an array of allocation IDs to be assigned to a specific server.
12-
*
13-
* @param int|null $server
14-
* @param array $ids
15-
* @return int
16-
*/
17-
public function assignAllocationsToServer(int $server = null, array $ids): int;
18-
19-
/**
20-
* Return all of the allocations for a specific node.
21-
*
22-
* @param int $node
23-
* @return \Illuminate\Support\Collection
24-
*/
25-
public function getAllocationsForNode(int $node): Collection;
26-
27-
/**
28-
* Return all of the allocations for a node in a paginated format.
29-
*
30-
* @param int $node
31-
* @param int $perPage
32-
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
33-
*/
34-
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator;
35-
369
/**
3710
* Return all of the unique IPs that exist for a given node.
3811
*

app/Http/Controllers/Api/Application/Nodes/AllocationController.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
44

55
use Pterodactyl\Models\Node;
6-
use Illuminate\Http\Response;
6+
use Illuminate\Http\JsonResponse;
77
use Pterodactyl\Models\Allocation;
88
use Pterodactyl\Services\Allocations\AssignmentService;
99
use Pterodactyl\Services\Allocations\AllocationDeletionService;
10-
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
1110
use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
1211
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
1312
use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
@@ -26,41 +25,32 @@ class AllocationController extends ApplicationApiController
2625
*/
2726
private $deletionService;
2827

29-
/**
30-
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
31-
*/
32-
private $repository;
33-
3428
/**
3529
* AllocationController constructor.
3630
*
3731
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
3832
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService
39-
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
4033
*/
4134
public function __construct(
4235
AssignmentService $assignmentService,
43-
AllocationDeletionService $deletionService,
44-
AllocationRepositoryInterface $repository
36+
AllocationDeletionService $deletionService
4537
) {
4638
parent::__construct();
4739

4840
$this->assignmentService = $assignmentService;
4941
$this->deletionService = $deletionService;
50-
$this->repository = $repository;
5142
}
5243

5344
/**
5445
* Return all of the allocations that exist for a given node.
5546
*
5647
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest $request
48+
* @param \Pterodactyl\Models\Node $node
5749
* @return array
5850
*/
59-
public function index(GetAllocationsRequest $request): array
51+
public function index(GetAllocationsRequest $request, Node $node): array
6052
{
61-
$allocations = $this->repository->getPaginatedAllocationsForNode(
62-
$request->getModel(Node::class)->id, 50
63-
);
53+
$allocations = $node->allocations()->paginate(50);
6454

6555
return $this->fractal->collection($allocations)
6656
->transformWith($this->getTransformer(AllocationTransformer::class))
@@ -71,32 +61,35 @@ public function index(GetAllocationsRequest $request): array
7161
* Store new allocations for a given node.
7262
*
7363
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest $request
74-
* @return \Illuminate\Http\Response
64+
* @param \Pterodactyl\Models\Node $node
65+
* @return \Illuminate\Http\JsonResponse
7566
*
7667
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
7768
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
7869
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
7970
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
8071
*/
81-
public function store(StoreAllocationRequest $request): Response
72+
public function store(StoreAllocationRequest $request, Node $node): JsonResponse
8273
{
83-
$this->assignmentService->handle($request->getModel(Node::class), $request->validated());
74+
$this->assignmentService->handle($node, $request->validated());
8475

85-
return response('', 204);
76+
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
8677
}
8778

8879
/**
8980
* Delete a specific allocation from the Panel.
9081
*
9182
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest $request
92-
* @return \Illuminate\Http\Response
83+
* @param \Pterodactyl\Models\Node $node
84+
* @param \Pterodactyl\Models\Allocation $allocation
85+
* @return \Illuminate\Http\JsonResponse
9386
*
9487
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
9588
*/
96-
public function delete(DeleteAllocationRequest $request): Response
89+
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse
9790
{
98-
$this->deletionService->handle($request->getModel(Allocation::class));
91+
$this->deletionService->handle($allocation);
9992

100-
return response('', 204);
93+
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
10194
}
10295
}

app/Repositories/Eloquent/AllocationRepository.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Support\Collection;
66
use Pterodactyl\Models\Allocation;
77
use Illuminate\Database\Eloquent\Builder;
8-
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
98
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
109

1110
class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface
@@ -20,41 +19,6 @@ public function model()
2019
return Allocation::class;
2120
}
2221

23-
/**
24-
* Set an array of allocation IDs to be assigned to a specific server.
25-
*
26-
* @param int|null $server
27-
* @param array $ids
28-
* @return int
29-
*/
30-
public function assignAllocationsToServer(int $server = null, array $ids): int
31-
{
32-
return $this->getBuilder()->whereIn('id', $ids)->update(['server_id' => $server]);
33-
}
34-
35-
/**
36-
* Return all of the allocations for a specific node.
37-
*
38-
* @param int $node
39-
* @return \Illuminate\Support\Collection
40-
*/
41-
public function getAllocationsForNode(int $node): Collection
42-
{
43-
return $this->getBuilder()->where('node_id', $node)->get($this->getColumns());
44-
}
45-
46-
/**
47-
* Return all of the allocations for a node in a paginated format.
48-
*
49-
* @param int $node
50-
* @param int $perPage
51-
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
52-
*/
53-
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator
54-
{
55-
return $this->getBuilder()->where('node_id', $node)->paginate($perPage, $this->getColumns());
56-
}
57-
5822
/**
5923
* Return all of the unique IPs that exist for a given node.
6024
*

app/Services/Servers/ServerCreationService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ private function storeAssignedAllocations(Server $server, array $data)
270270
$records = array_merge($records, $data['allocation_additional']);
271271
}
272272

273-
$this->allocationRepository->assignAllocationsToServer($server->id, $records);
273+
$this->allocationRepository->updateWhereIn('id', $records, [
274+
'server_id' => $server->id,
275+
]);
274276
}
275277

276278
/**

0 commit comments

Comments
 (0)