Update location and databasehost services to use repositories

Includes unit tests for both services
This commit is contained in:
Dane Everitt 2017-07-02 21:29:58 -05:00
parent 5c3dc60d1e
commit 50588a1f54
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 564 additions and 68 deletions

View file

@ -24,25 +24,23 @@
namespace Pterodactyl\Services;
use Pterodactyl\Models\Location;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationService
{
/**
* @var \Pterodactyl\Models\Location
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
*/
protected $model;
protected $repository;
/**
* LocationService constructor.
*
* @param \Pterodactyl\Models\Location $location
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
*/
public function __construct(Location $location)
public function __construct(LocationRepositoryInterface $repository)
{
$this->model = $location;
$this->repository = $repository;
}
/**
@ -55,13 +53,7 @@ class LocationService
*/
public function create(array $data)
{
$location = $this->model->newInstance($data);
if (! $location->save()) {
throw new DataValidationException($location->getValidator());
}
return $location;
return $this->repository->create($data);
}
/**
@ -75,13 +67,7 @@ class LocationService
*/
public function update($id, array $data)
{
$location = $this->model->findOrFail($id)->fill($data);
if (! $location->save()) {
throw new DataValidationException($location->getValidator());
}
return $location;
return $this->repository->update($id, $data);
}
/**
@ -94,12 +80,6 @@ class LocationService
*/
public function delete($id)
{
$location = $this->model->withCount('nodes')->findOrFail($id);
if ($location->nodes_count > 0) {
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
}
return $location->delete();
return $this->repository->deleteIfNoNodes($id);
}
}