Skip to content

Commit 50588a1

Browse files
committed
Update location and databasehost services to use repositories
Includes unit tests for both services
1 parent 5c3dc60 commit 50588a1

File tree

10 files changed

+564
-68
lines changed

10 files changed

+564
-68
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 DatabaseHostInterface extends RepositoryInterface
28+
{
29+
public function deleteIfNoDatabases($id);
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 LocationRepositoryInterface extends RepositoryInterface, SearchableInterface
30+
{
31+
public function deleteIfNoNodes($id);
32+
}

app/Extensions/DynamicDatabaseConnection.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace Pterodactyl\Extensions;
2626

27+
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
2728
use Pterodactyl\Models\DatabaseHost;
2829
use Illuminate\Contracts\Encryption\Encrypter;
2930
use Illuminate\Config\Repository as ConfigRepository;
@@ -45,25 +46,25 @@ class DynamicDatabaseConnection
4546
protected $encrypter;
4647

4748
/**
48-
* @var \Pterodactyl\Models\DatabaseHost
49+
* @var \Pterodactyl\Contracts\Repository\DatabaseHostInterface
4950
*/
50-
protected $model;
51+
protected $repository;
5152

5253
/**
5354
* DynamicDatabaseConnection constructor.
5455
*
55-
* @param \Illuminate\Config\Repository $config
56-
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
57-
* @param \Pterodactyl\Models\DatabaseHost $model
56+
* @param \Illuminate\Config\Repository $config
57+
* @param \Pterodactyl\Contracts\Repository\DatabaseHostInterface $repository
58+
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
5859
*/
5960
public function __construct(
6061
ConfigRepository $config,
61-
Encrypter $encrypter,
62-
DatabaseHost $model
62+
DatabaseHostInterface $repository,
63+
Encrypter $encrypter
6364
) {
6465
$this->config = $config;
6566
$this->encrypter = $encrypter;
66-
$this->model = $model;
67+
$this->repository = $repository;
6768
}
6869

6970
/**
@@ -76,7 +77,7 @@ public function __construct(
7677
public function set($connection, $host, $database = 'mysql')
7778
{
7879
if (! $host instanceof DatabaseHost) {
79-
$host = $this->model->findOrFail($host);
80+
$host = $this->repository->find($host);
8081
}
8182

8283
$this->config->set('database.connections.' . $connection, [

app/Providers/RepositoryServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
namespace Pterodactyl\Providers;
2626

2727
use Illuminate\Support\ServiceProvider;
28+
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
29+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
30+
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
31+
use Pterodactyl\Repositories\Eloquent\LocationRepository;
2832
use Pterodactyl\Repositories\Eloquent\UserRepository;
2933
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
3034

@@ -35,6 +39,8 @@ class RepositoryServiceProvider extends ServiceProvider
3539
*/
3640
public function register()
3741
{
42+
$this->app->bind(DatabaseHostInterface::class, DatabaseHostRepository::class);
43+
$this->app->bind(LocationRepositoryInterface::class, LocationRepository::class);
3844
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
3945
}
4046
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Repositories\Eloquent;
26+
27+
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
28+
use Pterodactyl\Exceptions\DisplayException;
29+
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
30+
use Pterodactyl\Models\DatabaseHost;
31+
32+
class DatabaseHostRepository extends EloquentRepository implements DatabaseHostInterface
33+
{
34+
/**
35+
* Setup the model to be used.
36+
*
37+
* @return string
38+
*/
39+
public function model()
40+
{
41+
return DatabaseHost::class;
42+
}
43+
44+
/**
45+
* Delete a database host from the DB if there are no databases using it.
46+
*
47+
* @param int $id
48+
* @return bool|null
49+
*
50+
* @throws \Pterodactyl\Exceptions\DisplayException
51+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
52+
*/
53+
public function deleteIfNoDatabases($id)
54+
{
55+
$instance = $this->getBuilder()->withCount('databases')->find($id);
56+
57+
if (! $instance) {
58+
throw new RecordNotFoundException();
59+
}
60+
61+
if ($instance->databases_count > 0) {
62+
throw new DisplayException('Cannot delete a database host that has active databases attached to it.');
63+
}
64+
65+
return $instance->delete();
66+
}
67+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Repositories\Eloquent;
26+
27+
use Pterodactyl\Models\Location;
28+
use Pterodactyl\Exceptions\DisplayException;
29+
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
30+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
31+
32+
class LocationRepository extends EloquentRepository implements LocationRepositoryInterface
33+
{
34+
/**
35+
* @var string
36+
*/
37+
protected $searchTerm;
38+
39+
/**
40+
* Setup model.
41+
*
42+
* @return string
43+
*/
44+
public function model()
45+
{
46+
return Location::class;
47+
}
48+
49+
/**
50+
* Setup the model for search abilities.
51+
*
52+
* @param $term
53+
* @return $this
54+
*/
55+
public function search($term)
56+
{
57+
if (empty($term)) {
58+
return $this;
59+
}
60+
61+
$clone = clone $this;
62+
$clone->searchTerm = $term;
63+
64+
return $clone;
65+
}
66+
67+
/**
68+
* Delete a location only if there are no nodes attached to it.
69+
*
70+
* @param $id
71+
* @return bool|mixed|null
72+
*
73+
* @throws \Pterodactyl\Exceptions\DisplayException
74+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
75+
*/
76+
public function deleteIfNoNodes($id)
77+
{
78+
$location = $this->getBuilder()->with('nodes')->find($id);
79+
80+
if (! $location) {
81+
throw new RecordNotFoundException();
82+
}
83+
84+
if ($location->nodes_count > 0) {
85+
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
86+
}
87+
88+
return $location->delete();
89+
}
90+
}

0 commit comments

Comments
 (0)