Update last of existing services to use repositories, includes unit tests

Also update PHPDocs on all the repository interfaces and classes to be correct.
This commit is contained in:
Dane Everitt 2017-07-08 14:07:51 -05:00
parent 50588a1f54
commit 0deb022093
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
21 changed files with 808 additions and 207 deletions

View file

@ -0,0 +1,30 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Contracts\Repository;
interface ApiKeyRepositoryInterface extends RepositoryInterface
{
//
}

View file

@ -0,0 +1,30 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Contracts\Repository;
interface ApiPermissionRepositoryInterface extends RepositoryInterface
{
//
}

View file

@ -26,5 +26,11 @@ namespace Pterodactyl\Contracts\Repository\Attributes;
interface SearchableInterface
{
/**
* Filter results by search term.
*
* @param string $term
* @return $this
*/
public function search($term);
}

View file

@ -26,5 +26,14 @@ namespace Pterodactyl\Contracts\Repository;
interface DatabaseHostInterface extends RepositoryInterface
{
/**
* Delete a database host from the DB if there are no databases using it.
*
* @param int $id
* @return bool|null
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function deleteIfNoDatabases($id);
}

View file

@ -28,5 +28,14 @@ use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
interface LocationRepositoryInterface extends RepositoryInterface, SearchableInterface
{
/**
* Delete a location only if there are no nodes attached to it.
*
* @param $id
* @return bool|mixed|null
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function deleteIfNoNodes($id);
}

View file

@ -26,25 +26,108 @@ namespace Pterodactyl\Contracts\Repository;
interface RepositoryInterface
{
/**
* Return an identifier or Model object to be used by the repository.
*
* @return string|\Closure|object
*/
public function model();
/**
* Return the model being used for this repository instance.
*
* @return mixed
*/
public function getModel();
/**
* Returns an instance of a query builder.
*
* @return mixed
*/
public function getBuilder();
/**
* Returns the colummns to be selected or returned by the query.
*
* @return mixed
*/
public function getColumns();
/**
* An array of columns to filter the response by.
*
* @param array $columns
* @return $this
*/
public function withColumns($columns = ['*']);
public function create($fields);
/**
* Disable returning a fresh model when data is inserted or updated.
*
* @return $this
*/
public function withoutFresh();
/**
* Create a new model instance and persist it to the database.
*
* @param array $fields
* @param bool $validate
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function create(array $fields, $validate = true);
/**
* Delete a given record from the database.
*
* @param int $id
* @return bool|null
*/
public function delete($id);
/**
* Find a model that has the specific ID passed.
*
* @param int $id
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function find($id);
public function findWhere($fields);
/**
* Find a model matching an array of where clauses.
*
* @param array $fields
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function findWhere(array $fields);
public function update($id, $fields);
/**
* Update a given ID with the passed array of fields.
*
* @param int $id
* @param array $fields
* @param bool $validate
* @param bool $force
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function update($id, array $fields, $validate = true, $force = false);
public function massUpdate($fields);
/**
* Update multiple records matching the passed clauses.
*
* @param array $where
* @param array $fields
* @return mixed
*/
public function massUpdate(array $where, array $fields);
}

View file

@ -28,7 +28,20 @@ use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
interface UserRepositoryInterface extends RepositoryInterface, SearchableInterface
{
/**
* Return all users with counts of servers and subusers of servers.
*
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function getAllUsersWithCounts();
/**
* Delete a user if they have no servers attached to their account.
*
* @param int $id
* @return bool
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function deleteIfNoServers($id);
}

View file

@ -36,7 +36,7 @@ class APIPermission extends Model implements ValidableContract
/**
* List of permissions available for the API.
*/
const PERMISSIONS = [
const CONST_PERMISSIONS = [
// Items within this block are available to non-adminitrative users.
'_user' => [
'server' => [

View file

@ -25,8 +25,12 @@
namespace Pterodactyl\Providers;
use Illuminate\Support\ServiceProvider;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\ApiPermissionRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
use Pterodactyl\Repositories\Eloquent\ApiPermissionRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\UserRepository;
@ -39,6 +43,8 @@ class RepositoryServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class);
$this->app->bind(ApiPermissionRepositoryInterface::class, ApiPermissionRepository::class);
$this->app->bind(DatabaseHostInterface::class, DatabaseHostRepository::class);
$this->app->bind(LocationRepositoryInterface::class, LocationRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);

View file

@ -0,0 +1,39 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\APIKey;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function model()
{
return APIKey::class;
}
}

View file

@ -0,0 +1,39 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\APIPermission;
use Pterodactyl\Contracts\Repository\ApiPermissionRepositoryInterface;
class ApiPermissionRepository extends EloquentRepository implements ApiPermissionRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function model()
{
return APIPermission::class;
}
}

View file

@ -32,9 +32,7 @@ use Pterodactyl\Models\DatabaseHost;
class DatabaseHostRepository extends EloquentRepository implements DatabaseHostInterface
{
/**
* Setup the model to be used.
*
* @return string
* {@inheritdoc}
*/
public function model()
{
@ -42,13 +40,7 @@ class DatabaseHostRepository extends EloquentRepository implements DatabaseHostI
}
/**
* Delete a database host from the DB if there are no databases using it.
*
* @param int $id
* @return bool|null
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* {@inheritdoc}
*/
public function deleteIfNoDatabases($id)
{

View file

@ -24,14 +24,15 @@
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Repository\Repository;
use Pterodactyl\Contracts\Repository\RepositoryInterface;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
abstract class EloquentRepository extends Repository implements RepositoryInterface
{
/**
* {@inheritdoc}
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getBuilder()
@ -40,14 +41,11 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
}
/**
* Create a new model instance and persist it to the database.
* @param array $fields
* @param bool $validate
* @param bool $force
* @return bool|\Illuminate\Database\Eloquent\Model
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* {@inheritdoc}
* @param bool $force
* @return \Illuminate\Database\Eloquent\Model|bool
*/
public function create($fields, $validate = true, $force = false)
public function create(array $fields, $validate = true, $force = false)
{
$instance = $this->getBuilder()->newModelInstance();
@ -69,12 +67,8 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
}
/**
* Return a record from the database for a given ID.
*
* @param int $id
* {@inheritdoc}
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function find($id)
{
@ -87,17 +81,16 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
return $instance;
}
public function findWhere($fields)
/**
* {@inheritdoc}
*/
public function findWhere(array $fields)
{
// TODO: Implement findWhere() method.
}
/**
* Delete a record from the DB given an ID.
*
* @param int $id
* @param bool $destroy
* @return bool|null
* {@inheritdoc}
*/
public function delete($id, $destroy = false)
{
@ -109,16 +102,9 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
}
/**
* @param int $id
* @param array $fields
* @param bool $validate
* @param bool $force
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* {@inheritdoc}
*/
public function update($id, $fields, $validate = true, $force = false)
public function update($id, array $fields, $validate = true, $force = false)
{
$instance = $this->getBuilder()->where('id', $id)->first();
@ -143,7 +129,10 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
return ($this->withFresh) ? $instance->fresh($this->getColumns()) : $saved;
}
public function massUpdate($fields)
/**
* {@inheritdoc}
*/
public function massUpdate(array $where, array $fields)
{
// TODO: Implement massUpdate() method.
}

View file

@ -37,9 +37,7 @@ class LocationRepository extends EloquentRepository implements LocationRepositor
protected $searchTerm;
/**
* Setup model.
*
* @return string
* {@inheritdoc}
*/
public function model()
{
@ -47,10 +45,7 @@ class LocationRepository extends EloquentRepository implements LocationRepositor
}
/**
* Setup the model for search abilities.
*
* @param $term
* @return $this
* {@inheritdoc}
*/
public function search($term)
{
@ -65,13 +60,7 @@ class LocationRepository extends EloquentRepository implements LocationRepositor
}
/**
* Delete a location only if there are no nodes attached to it.
*
* @param $id
* @return bool|mixed|null
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* {@inheritdoc}
*/
public function deleteIfNoNodes($id)
{

View file

@ -56,11 +56,17 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function model()
{
return User::class;
}
/**
* {@inheritdoc}
*/
public function search($term)
{
if (empty($term)) {
@ -73,6 +79,9 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
return $clone;
}
/**
* {@inheritdoc}
*/
public function getAllUsersWithCounts()
{
$users = $this->getBuilder()->withCount('servers', 'subuserOf');
@ -87,12 +96,7 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
}
/**
* Delete a user if they have no servers attached to their account.
*
* @param int $id
* @return bool
*
* @throws \Pterodactyl\Exceptions\DisplayException
* {@inheritdoc}
*/
public function deleteIfNoServers($id)
{

View file

@ -24,48 +24,52 @@
namespace Pterodactyl\Services;
use Pterodactyl\Models\APIKey;
use Illuminate\Database\Connection;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class ApiKeyService
{
const PUB_CRYPTO_BYTES = 8;
const PRIV_CRYPTO_BYTES = 32;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $database;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
* @var \Pterodactyl\Models\APIKey
*/
protected $model;
/**
* @var \Pterodactyl\Services\ApiPermissionService
*/
protected $permissionService;
/**
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
*/
protected $repository;
/**
* ApiKeyService constructor.
*
* @param \Pterodactyl\Models\APIKey $model
* @param \Illuminate\Database\Connection $database
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param \Pterodactyl\Services\ApiPermissionService $permissionService
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
* @param \Illuminate\Database\ConnectionInterface $database
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param \Pterodactyl\Services\ApiPermissionService $permissionService
*/
public function __construct(
APIKey $model,
Connection $database,
ApiKeyRepositoryInterface $repository,
ConnectionInterface $database,
Encrypter $encrypter,
ApiPermissionService $permissionService
) {
$this->repository = $repository;
$this->database = $database;
$this->encrypter = $encrypter;
$this->model = $model;
$this->permissionService = $permissionService;
}
@ -88,16 +92,12 @@ class ApiKeyService
// Start a Transaction
$this->database->beginTransaction();
$instance = $this->model->newInstance($data);
$instance->public = $publicKey;
$instance->secret = $this->encrypter->encrypt($secretKey);
$data = array_merge($data, [
'public' => $publicKey,
'secret' => $this->encrypter->encrypt($secretKey),
]);
if (! $instance->save()) {
$this->database->rollBack();
throw new DataValidationException($instance->getValidator());
}
$key = $instance->fresh();
$instance = $this->repository->create($data, true, true);
$nodes = $this->permissionService->getPermissions();
foreach ($permissions as $permission) {
@ -111,7 +111,7 @@ class ApiKeyService
continue;
}
$this->permissionService->create($key->id, sprintf('user.%s', $permission));
$this->permissionService->create($instance->id, sprintf('user.%s', $permission));
}
foreach ($administrative as $permission) {
@ -125,7 +125,7 @@ class ApiKeyService
continue;
}
$this->permissionService->create($key->id, $permission);
$this->permissionService->create($instance->id, $permission);
}
$this->database->commit();
@ -136,18 +136,11 @@ class ApiKeyService
/**
* Delete the API key and associated permissions from the database.
*
* @param int|\Pterodactyl\Models\APIKey $key
* @param int $id
* @return bool|null
*
* @throws \Exception
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function revoke($key)
public function revoke($id)
{
if (! $key instanceof APIKey) {
$key = $this->model->findOrFail($key);
}
return $key->delete();
return $this->repository->delete($id);
}
}

View file

@ -24,24 +24,23 @@
namespace Pterodactyl\Services;
use Pterodactyl\Models\APIPermission;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Contracts\Repository\ApiPermissionRepositoryInterface;
class ApiPermissionService
{
/**
* @var \Pterodactyl\Models\APIPermission
* @var \Pterodactyl\Contracts\Repository\ApiPermissionRepositoryInterface
*/
protected $model;
protected $repository;
/**
* ApiPermissionService constructor.
*
* @param \Pterodactyl\Models\APIPermission $model
* @param \Pterodactyl\Contracts\Repository\ApiPermissionRepositoryInterface $repository
*/
public function __construct(APIPermission $model)
public function __construct(ApiPermissionRepositoryInterface $repository)
{
$this->model = $model;
$this->repository = $repository;
}
/**
@ -55,16 +54,11 @@ class ApiPermissionService
*/
public function create($key, $permission)
{
$instance = $this->model->newInstance([
// @todo handle an array of permissions to do a mass assignment?
return $this->repository->withoutFresh()->create([
'key_id' => $key,
'permission' => $permission,
]);
if (! $instance->save()) {
throw new DataValidationException($instance->getValidator());
}
return true;
}
/**
@ -74,6 +68,6 @@ class ApiPermissionService
*/
public function getPermissions()
{
return APIPermission::PERMISSIONS;
return $this->repository->getModel()::CONST_PERMISSIONS;
}
}