Service refactor to improve organization

This commit is contained in:
Dane Everitt 2017-07-08 15:04:59 -05:00
parent 761d34f178
commit 2588c25b0b
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
12 changed files with 79 additions and 42 deletions

View File

@ -108,6 +108,14 @@ interface RepositoryInterface
*/
public function findWhere(array $fields);
/**
* Find and return the first matching instance for the given fields.
*
* @param array $fields
* @return mixed
*/
public function findFirstWhere(array $fields);
/**
* Update a given ID with the passed array of fields.
*

View File

@ -28,7 +28,7 @@ use Pterodactyl\Models\Location;
use Pterodactyl\Models\DatabaseHost;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\DatabaseHostService;
use Pterodactyl\Services\Administrative\DatabaseHostService;
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
class DatabaseController extends Controller
@ -49,17 +49,17 @@ class DatabaseController extends Controller
protected $locationModel;
/**
* @var \Pterodactyl\Services\DatabaseHostService
* @var \Pterodactyl\Services\Administrative\DatabaseHostService
*/
protected $service;
/**
* DatabaseController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Models\DatabaseHost $hostModel
* @param \Pterodactyl\Models\Location $locationModel
* @param \Pterodactyl\Services\DatabaseHostService $service
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Models\DatabaseHost $hostModel
* @param \Pterodactyl\Models\Location $locationModel
* @param \Pterodactyl\Services\Administrative\DatabaseHostService $service
*/
public function __construct(
AlertsMessageBag $alert,

View File

@ -26,10 +26,10 @@ namespace Pterodactyl\Http\Controllers\Admin;
use Pterodactyl\Models\Location;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Services\LocationService;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\LocationRequest;
use Pterodactyl\Services\Administrative\LocationService;
class LocationController extends Controller
{
@ -44,16 +44,16 @@ class LocationController extends Controller
protected $locationModel;
/**
* @var \Pterodactyl\Services\LocationService
* @var \Pterodactyl\Services\Administrative\\LocationService
*/
protected $service;
/**
* LocationController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Models\Location $locationModel
* @param \Pterodactyl\Services\LocationService $service
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Models\Location $locationModel
* @param \Pterodactyl\Services\Administrative\LocationService $service
*/
public function __construct(
AlertsMessageBag $alert,

View File

@ -28,7 +28,7 @@ use Illuminate\Http\Request;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Models\User;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Services\UserService;
use Pterodactyl\Services\Administrative\UserService;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\UserFormRequest;
@ -41,7 +41,7 @@ class UserController extends Controller
protected $alert;
/**
* @var \Pterodactyl\Services\UserService
* @var \Pterodactyl\Services\Administrative\UserService
*/
protected $service;
@ -59,7 +59,7 @@ class UserController extends Controller
* UserController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Services\UserService $service
* @param \Pterodactyl\Services\Administrative\UserService $service
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
* @param \Pterodactyl\Models\User $model
*/

View File

@ -26,12 +26,13 @@
namespace Pterodactyl\Http\Controllers\Base;
use Illuminate\Http\Request;
use Pterodactyl\Models\APIKey;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Models\APIPermission;
use Pterodactyl\Services\ApiKeyService;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\ApiKeyRequest;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class APIController extends Controller
{
@ -41,9 +42,9 @@ class APIController extends Controller
protected $alert;
/**
* @var \Pterodactyl\Models\APIKey
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
*/
protected $model;
protected $repository;
/**
* @var \Pterodactyl\Services\ApiKeyService
@ -53,13 +54,17 @@ class APIController extends Controller
/**
* APIController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Services\ApiKeyService $service
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
* @param \Pterodactyl\Services\ApiKeyService $service
*/
public function __construct(AlertsMessageBag $alert, ApiKeyService $service, APIKey $model)
{
public function __construct(
AlertsMessageBag $alert,
ApiKeyRepositoryInterface $repository,
ApiKeyService $service
) {
$this->alert = $alert;
$this->model = $model;
$this->repository = $repository;
$this->service = $service;
}
@ -72,7 +77,7 @@ class APIController extends Controller
public function index(Request $request)
{
return view('base.api.index', [
'keys' => APIKey::where('user_id', $request->user()->id)->get(),
'keys' => $this->repository->findWhere([['user_id', '=', $request->user()->id]]),
]);
}
@ -85,8 +90,8 @@ class APIController extends Controller
{
return view('base.api.new', [
'permissions' => [
'user' => collect(APIPermission::PERMISSIONS)->pull('_user'),
'admin' => collect(APIPermission::PERMISSIONS)->except('_user')->toArray(),
'user' => collect(APIPermission::CONST_PERMISSIONS)->pull('_user'),
'admin' => collect(APIPermission::CONST_PERMISSIONS)->except('_user')->toArray(),
],
]);
}
@ -113,7 +118,11 @@ class APIController extends Controller
'memo' => $request->input('memo'),
], $request->input('permissions') ?? [], $adminPermissions);
$this->alert->success('An API Key-Pair has successfully been generated. The API secret for this public key is shown below and will not be shown again.<br /><br /><code>' . $secret . '</code>')->flash();
$this->alert->success(
"An API Key-Pair has successfully been generated. The API secret
for this public key is shown below and will not be shown again.
<br /><br /><code>{$secret}</code>"
)->flash();
return redirect()->route('account.api');
}
@ -127,12 +136,16 @@ class APIController extends Controller
*/
public function revoke(Request $request, $key)
{
$key = $this->model->newQuery()
->where('user_id', $request->user()->id)
->where('public', $key)
->firstOrFail();
try {
$key = $this->repository->withColumns('id')->findFirstWhere([
['user_id', '=', $request->user()->id],
['public', $key],
]);
$this->service->revoke($key);
$this->service->revoke($key->id);
} catch (RecordNotFoundException $ex) {
return abort(404);
}
return response('', 204);
}

View File

@ -83,10 +83,26 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
/**
* {@inheritdoc}
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findWhere(array $fields)
{
// TODO: Implement findWhere() method.
return $this->getBuilder()->where($fields)->get($this->getColumns());
}
/**
* {@inheritdoc}
* @return \Illuminate\Database\Eloquent\Model
*/
public function findFirstWhere(array $fields)
{
$instance = $this->getBuilder()->where($fields)->first($this->getColumns());
if (! $instance) {
throw new RecordNotFoundException();
}
return $instance;
}
/**

View File

@ -22,7 +22,7 @@
* SOFTWARE.
*/
namespace Pterodactyl\Services;
namespace Pterodactyl\Services\Administrative;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter;

View File

@ -22,7 +22,7 @@
* SOFTWARE.
*/
namespace Pterodactyl\Services;
namespace Pterodactyl\Services\Administrative;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;

View File

@ -22,7 +22,7 @@
* SOFTWARE.
*/
namespace Pterodactyl\Services;
namespace Pterodactyl\Services\Administrative;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Hashing\Hasher;

View File

@ -22,15 +22,15 @@
* SOFTWARE.
*/
namespace Tests\Unit\Services;
namespace Tests\Unit\Services\Administrative;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Services\DatabaseHostService;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
use Pterodactyl\Services\Administrative\DatabaseHostService;
class DatabaseHostServiceTest extends TestCase
{
@ -55,7 +55,7 @@ class DatabaseHostServiceTest extends TestCase
protected $repository;
/**
* @var \Pterodactyl\Services\DatabaseHostService
* @var \Pterodactyl\Services\Administrative\DatabaseHostService
*/
protected $service;

View File

@ -26,7 +26,7 @@ namespace Tests\Unit\Services;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Services\LocationService;
use Pterodactyl\Services\Administrative\LocationService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationServiceTest extends TestCase
@ -37,7 +37,7 @@ class LocationServiceTest extends TestCase
protected $repository;
/**
* @var \Pterodactyl\Services\LocationService
* @var \Pterodactyl\Services\Administrative\LocationService
*/
protected $service;

View File

@ -26,12 +26,12 @@ namespace Tests\Unit\Services;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Services\UserService;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Notifications\ChannelManager;
use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Services\Administrative\UserService;
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
@ -68,7 +68,7 @@ class UserServiceTest extends TestCase
protected $repository;
/**
* @var \Pterodactyl\Services\UserService
* @var \Pterodactyl\Services\Administrative\UserService
*/
protected $service;