More repository/service/refactor changes

This commit is contained in:
Dane Everitt 2017-08-12 15:29:01 -05:00
parent 2c77d5c44d
commit b8d7d99096
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
44 changed files with 977 additions and 669 deletions

View file

@ -24,8 +24,9 @@
namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Database\Query\Expression;
use Webmozart\Assert\Assert;
use Pterodactyl\Repository\Repository;
use Illuminate\Database\Query\Expression;
use Pterodactyl\Contracts\Repository\RepositoryInterface;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
@ -48,6 +49,9 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function create(array $fields, $validate = true, $force = false)
{
Assert::boolean($validate, 'Second argument passed to create should be boolean, recieved %s.');
Assert::boolean($force, 'Third argument passed to create should be boolean, received %s.');
$instance = $this->getBuilder()->newModelInstance();
if ($force) {
@ -73,6 +77,8 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function find($id)
{
Assert::integer($id, 'First argument passed to find should be integer, received %s.');
$instance = $this->getBuilder()->find($id, $this->getColumns());
if (! $instance) {
@ -119,6 +125,9 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function delete($id, $destroy = false)
{
Assert::integer($id, 'First argument passed to delete should be integer, received %s.');
Assert::boolean($destroy, 'Second argument passed to delete should be boolean, received %s.');
$instance = $this->getBuilder()->where($this->getModel()->getKeyName(), $id);
return ($destroy) ? $instance->forceDelete() : $instance->delete();
@ -129,6 +138,8 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function deleteWhere(array $attributes, $force = false)
{
Assert::boolean($force, 'Second argument passed to deleteWhere should be boolean, received %s.');
$instance = $this->getBuilder()->where($attributes);
return ($force) ? $instance->forceDelete() : $instance->delete();
@ -139,6 +150,10 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function update($id, array $fields, $validate = true, $force = false)
{
Assert::integer($id, 'First argument passed to update expected to be integer, received %s.');
Assert::boolean($validate, 'Third argument passed to update should be boolean, received %s.');
Assert::boolean($force, 'Fourth argument passed to update should be boolean, received %s.');
$instance = $this->getBuilder()->where('id', $id)->first();
if (! $instance) {
@ -167,6 +182,8 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function updateWhereIn($column, array $values, array $fields)
{
Assert::stringNotEmpty($column, 'First argument passed to updateWhereIn expected to be a string, received %s.');
return $this->getBuilder()->whereIn($column, $values)->update($fields);
}
@ -238,6 +255,9 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function updateOrCreate(array $where, array $fields, $validate = true, $force = false)
{
Assert::boolean($validate, 'Third argument passed to updateOrCreate should be boolean, received %s.');
Assert::boolean($force, 'Fourth argument passed to updateOrCreate should be boolean, received %s.');
$instance = $this->withColumns('id')->findWhere($where)->first();
if (! $instance) {

View file

@ -36,4 +36,31 @@ class ServiceOptionRepository extends EloquentRepository implements ServiceOptio
{
return ServiceOption::class;
}
/**
* {@inheritdoc}
*/
public function getWithVariables($id)
{
return $this->getBuilder()->with('variables')->find($id, $this->getColumns());
}
/**
* {@inheritdoc}
*/
public function getWithCopyFrom($id)
{
return $this->getBuilder()->with('copyFrom')->find($id, $this->getColumns());
}
/**
* {@inheritdoc}
*/
public function isCopiableScript($copyFromId, $service)
{
return $this->getBuilder()->whereNull('copy_script_from')
->where('id', '=', $copyFromId)
->where('service_id', '=', $service)
->exists();
}
}

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\ServiceVariable;
use Pterodactyl\Contracts\Repository\ServiceVariableRepositoryInterface;
class ServiceVariableRepository extends EloquentRepository implements ServiceVariableRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function model()
{
return ServiceVariable::class;
}
}