Move services onto new services system, includes tests

This commit is contained in:
Dane Everitt 2017-08-15 22:21:47 -05:00
parent e91079d128
commit 90bbe57148
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
26 changed files with 899 additions and 272 deletions

View file

@ -49,8 +49,8 @@ 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.');
Assert::boolean($validate, 'Second argument passed to create must be boolean, recieved %s.');
Assert::boolean($force, 'Third argument passed to create must be boolean, received %s.');
$instance = $this->getBuilder()->newModelInstance();
@ -77,7 +77,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/
public function find($id)
{
Assert::integer($id, 'First argument passed to find should be integer, received %s.');
Assert::numeric($id, 'First argument passed to find must be numeric, received %s.');
$instance = $this->getBuilder()->find($id, $this->getColumns());
@ -125,8 +125,8 @@ 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.');
Assert::numeric($id, 'First argument passed to delete must be numeric, received %s.');
Assert::boolean($destroy, 'Second argument passed to delete must be boolean, received %s.');
$instance = $this->getBuilder()->where($this->getModel()->getKeyName(), $id);
@ -138,7 +138,7 @@ 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.');
Assert::boolean($force, 'Second argument passed to deleteWhere must be boolean, received %s.');
$instance = $this->getBuilder()->where($attributes);
@ -150,9 +150,9 @@ 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.');
Assert::numeric($id, 'First argument passed to update must be numeric, received %s.');
Assert::boolean($validate, 'Third argument passed to update must be boolean, received %s.');
Assert::boolean($force, 'Fourth argument passed to update must be boolean, received %s.');
$instance = $this->getBuilder()->where('id', $id)->first();
@ -182,7 +182,7 @@ 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.');
Assert::stringNotEmpty($column, 'First argument passed to updateWhereIn must be a non-empty string, received %s.');
return $this->getBuilder()->whereIn($column, $values)->update($fields);
}
@ -255,8 +255,8 @@ 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.');
Assert::boolean($validate, 'Third argument passed to updateOrCreate must be boolean, received %s.');
Assert::boolean($force, 'Fourth argument passed to updateOrCreate must be boolean, received %s.');
$instance = $this->withColumns('id')->findWhere($where)->first();

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Repositories\Eloquent;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Service;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface;
@ -43,11 +44,12 @@ class ServiceRepository extends EloquentRepository implements ServiceRepositoryI
*/
public function getWithOptions($id = null)
{
Assert::nullOrNumeric($id, 'First argument passed to getWithOptions must be null or numeric, received %s.');
$instance = $this->getBuilder()->with('options.packs', 'options.variables');
if (! is_null($id)) {
$instance = $instance->find($id, $this->getColumns());
if (! $instance) {
throw new RecordNotFoundException();
}
@ -57,4 +59,19 @@ class ServiceRepository extends EloquentRepository implements ServiceRepositoryI
return $instance->get($this->getColumns());
}
/**
* {@inheritdoc}
*/
public function getWithOptionServers($id)
{
Assert::numeric($id, 'First argument passed to getWithOptionServers must be numeric, received %s.');
$instance = $this->getBuilder()->with('options.servers')->find($id, $this->getColumns());
if (! $instance) {
throw new RecordNotFoundException();
}
return $instance;
}
}

View file

@ -1,135 +0,0 @@
<?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;
use DB;
use Validator;
use Pterodactyl\Models\Service;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
class ServiceRepository
{
/**
* Creates a new service on the system.
*
* @param array $data
* @return \Pterodactyl\Models\Service
*
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function create(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|string|min:1|max:255',
'description' => 'required|nullable|string',
'folder' => 'required|unique:services,folder|regex:/^[\w.-]{1,50}$/',
'startup' => 'required|nullable|string',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
return DB::transaction(function () use ($data) {
$service = new Service;
$service->author = config('pterodactyl.service.author');
$service->fill([
'name' => $data['name'],
'description' => (isset($data['description'])) ? $data['description'] : null,
'folder' => $data['folder'],
'startup' => (isset($data['startup'])) ? $data['startup'] : null,
'index_file' => Service::defaultIndexFile(),
])->save();
// It is possible for an event to return false or throw an exception
// which won't necessarily be detected by this transaction.
//
// This check ensures the model was actually saved.
if (! $service->exists) {
throw new \Exception('Service model was created however the response appears to be invalid. Did an event fire wrongly?');
}
return $service;
});
}
/**
* Updates a service.
*
* @param int $id
* @param array $data
* @return \Pterodactyl\Models\Service
*
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function update($id, array $data)
{
$service = Service::findOrFail($id);
$validator = Validator::make($data, [
'name' => 'sometimes|required|string|min:1|max:255',
'description' => 'sometimes|required|nullable|string',
'folder' => 'sometimes|required|regex:/^[\w.-]{1,50}$/',
'startup' => 'sometimes|required|nullable|string',
'index_file' => 'sometimes|required|string',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
return DB::transaction(function () use ($data, $service) {
$service->fill($data)->save();
return $service;
});
}
/**
* Deletes a service and associated files and options.
*
* @param int $id
* @return void
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function delete($id)
{
$service = Service::withCount('servers')->with('options')->findOrFail($id);
if ($service->servers_count > 0) {
throw new DisplayException('You cannot delete a service that has servers associated with it.');
}
DB::transaction(function () use ($service) {
foreach ($service->options as $option) {
(new OptionRepository)->delete($option->id);
}
$service->delete();
});
}
}