Skip to content

Commit a498bbc

Browse files
committed
Move location service to match other services
1 parent a8560b7 commit a498bbc

File tree

14 files changed

+414
-149
lines changed

14 files changed

+414
-149
lines changed

app/Console/Commands/AddLocation.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
namespace Pterodactyl\Console\Commands;
2626

2727
use Illuminate\Console\Command;
28-
use Pterodactyl\Repositories\LocationRepository;
2928

3029
class AddLocation extends Command
3130
{

app/Console/Commands/Inspire.php

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

app/Console/Kernel.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class Kernel extends ConsoleKernel
1313
* @var array
1414
*/
1515
protected $commands = [
16-
\Pterodactyl\Console\Commands\Inspire::class,
1716
\Pterodactyl\Console\Commands\MakeUser::class,
1817
\Pterodactyl\Console\Commands\ShowVersion::class,
1918
\Pterodactyl\Console\Commands\UpdateEnvironment::class,

app/Contracts/Repository/LocationRepositoryInterface.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@
2828

2929
interface LocationRepositoryInterface extends RepositoryInterface, SearchableInterface
3030
{
31-
/**
32-
* Delete a location only if there are no nodes attached to it.
33-
*
34-
* @param $id
35-
* @return bool|mixed|null
36-
*
37-
* @throws \Pterodactyl\Exceptions\DisplayException
38-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
39-
*/
40-
public function deleteIfNoNodes($id);
41-
4231
/**
4332
* Return locations with a count of nodes and servers attached to it.
4433
*
@@ -62,4 +51,14 @@ public function getAllWithNodes();
6251
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6352
*/
6453
public function getWithNodes($id);
54+
55+
/**
56+
* Return a location and the count of nodes in that location.
57+
*
58+
* @param int $id
59+
* @return mixed
60+
*
61+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
62+
*/
63+
public function getWithNodeCount($id);
6564
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Exceptions\Service\Location;
26+
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
class HasActiveNodesException extends DisplayException
30+
{
31+
}

app/Http/Controllers/Admin/LocationController.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626

2727
use Pterodactyl\Models\Location;
2828
use Prologue\Alerts\AlertsMessageBag;
29-
use Pterodactyl\Services\LocationService;
3029
use Pterodactyl\Exceptions\DisplayException;
3130
use Pterodactyl\Http\Controllers\Controller;
3231
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
32+
use Pterodactyl\Services\Locations\LocationUpdateService;
33+
use Pterodactyl\Services\Locations\LocationCreationService;
34+
use Pterodactyl\Services\Locations\LocationDeletionService;
3335
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
3436

3537
class LocationController extends Controller
@@ -39,31 +41,47 @@ class LocationController extends Controller
3941
*/
4042
protected $alert;
4143

44+
/**
45+
* @var \Pterodactyl\Services\Locations\LocationCreationService
46+
*/
47+
protected $creationService;
48+
49+
/**
50+
* @var \Pterodactyl\Services\Locations\LocationDeletionService
51+
*/
52+
protected $deletionService;
53+
4254
/**
4355
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
4456
*/
4557
protected $repository;
4658

4759
/**
48-
* @var \Pterodactyl\Services\\LocationService
60+
* @var \Pterodactyl\Services\Locations\LocationUpdateService
4961
*/
50-
protected $service;
62+
protected $updateService;
5163

5264
/**
5365
* LocationController constructor.
5466
*
5567
* @param \Prologue\Alerts\AlertsMessageBag $alert
68+
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
69+
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
5670
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
57-
* @param \Pterodactyl\Services\LocationService $service
71+
* @param \Pterodactyl\Services\Locations\LocationUpdateService $updateService
5872
*/
5973
public function __construct(
6074
AlertsMessageBag $alert,
75+
LocationCreationService $creationService,
76+
LocationDeletionService $deletionService,
6177
LocationRepositoryInterface $repository,
62-
LocationService $service
78+
LocationUpdateService $updateService
6379
) {
6480
$this->alert = $alert;
81+
$this->creationService = $creationService;
82+
$this->deletionService = $deletionService;
6583
$this->repository = $repository;
66-
$this->service = $service;
84+
$this->updateService = $updateService;
6785
}
6886

6987
/**
@@ -104,7 +122,7 @@ public function view($id)
104122
*/
105123
public function create(LocationFormRequest $request)
106124
{
107-
$location = $this->service->create($request->normalize());
125+
$location = $this->creationService->handle($request->normalize());
108126
$this->alert->success('Location was created successfully.')->flash();
109127

110128
return redirect()->route('admin.locations.view', $location->id);
@@ -126,7 +144,7 @@ public function update(LocationFormRequest $request, Location $location)
126144
return $this->delete($location);
127145
}
128146

129-
$this->service->update($location->id, $request->normalize());
147+
$this->updateService->handle($location->id, $request->normalize());
130148
$this->alert->success('Location was updated successfully.')->flash();
131149

132150
return redirect()->route('admin.locations.view', $location->id);
@@ -144,7 +162,7 @@ public function update(LocationFormRequest $request, Location $location)
144162
public function delete(Location $location)
145163
{
146164
try {
147-
$this->service->delete($location->id);
165+
$this->deletionService->handle($location->id);
148166

149167
return redirect()->route('admin.locations');
150168
} catch (DisplayException $ex) {

app/Repositories/Eloquent/LocationRepository.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
namespace Pterodactyl\Repositories\Eloquent;
2626

2727
use Pterodactyl\Models\Location;
28-
use Pterodactyl\Exceptions\DisplayException;
2928
use Pterodactyl\Repositories\Concerns\Searchable;
3029
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
3130
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
@@ -47,25 +46,6 @@ public function model()
4746
return Location::class;
4847
}
4948

50-
/**
51-
* {@inheritdoc}
52-
* @todo remove this, do logic in service
53-
*/
54-
public function deleteIfNoNodes($id)
55-
{
56-
$location = $this->getBuilder()->with('nodes')->find($id);
57-
58-
if (! $location) {
59-
throw new RecordNotFoundException();
60-
}
61-
62-
if ($location->nodes_count > 0) {
63-
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
64-
}
65-
66-
return $location->delete();
67-
}
68-
6949
/**
7050
* {@inheritdoc}
7151
*/
@@ -88,9 +68,21 @@ public function getAllWithNodes()
8868
public function getWithNodes($id)
8969
{
9070
$instance = $this->getBuilder()->with('nodes.servers')->find($id, $this->getColumns());
71+
if (! $instance) {
72+
throw new RecordNotFoundException;
73+
}
74+
75+
return $instance;
76+
}
9177

78+
/**
79+
* {@inheritdoc}
80+
*/
81+
public function getWithNodeCount($id)
82+
{
83+
$instance = $this->getBuilder()->withCount('nodes')->find($id, $this->getColumns());
9284
if (! $instance) {
93-
throw new RecordNotFoundException();
85+
throw new RecordNotFoundException;
9486
}
9587

9688
return $instance;

app/Services/LocationService.php renamed to app/Services/Locations/LocationCreationService.php

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

25-
namespace Pterodactyl\Services;
25+
namespace Pterodactyl\Services\Locations;
2626

2727
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
2828

29-
class LocationService
29+
class LocationCreationService
3030
{
3131
/**
3232
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
3333
*/
3434
protected $repository;
3535

3636
/**
37-
* LocationService constructor.
37+
* LocationCreationService constructor.
3838
*
3939
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
4040
*/
@@ -44,42 +44,15 @@ public function __construct(LocationRepositoryInterface $repository)
4444
}
4545

4646
/**
47-
* Create the location in the database and return it.
47+
* Create a new location.
4848
*
4949
* @param array $data
5050
* @return \Pterodactyl\Models\Location
5151
*
5252
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
5353
*/
54-
public function create(array $data)
54+
public function handle(array $data)
5555
{
5656
return $this->repository->create($data);
5757
}
58-
59-
/**
60-
* Update location model in the DB.
61-
*
62-
* @param int $id
63-
* @param array $data
64-
* @return \Pterodactyl\Models\Location
65-
*
66-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
67-
*/
68-
public function update($id, array $data)
69-
{
70-
return $this->repository->update($id, $data);
71-
}
72-
73-
/**
74-
* Delete a model from the DB.
75-
*
76-
* @param int $id
77-
* @return bool
78-
*
79-
* @throws \Pterodactyl\Exceptions\DisplayException
80-
*/
81-
public function delete($id)
82-
{
83-
return $this->repository->deleteIfNoNodes($id);
84-
}
8558
}

0 commit comments

Comments
 (0)