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,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)
{