Remove egg variable repository

This commit is contained in:
Lance Pioch 2024-03-16 22:13:13 -04:00
parent 50fa260a38
commit 449f875d20
10 changed files with 16 additions and 168 deletions

View File

@ -1,14 +0,0 @@
<?php
namespace App\Contracts\Repository;
use Illuminate\Support\Collection;
interface EggVariableRepositoryInterface extends RepositoryInterface
{
/**
* Return editable variables for a given egg. Editable variables must be set to
* user viewable in order to be picked up by this function.
*/
public function getEditableVariables(int $egg): Collection;
}

View File

@ -1,25 +0,0 @@
<?php
namespace App\Contracts\Repository;
use App\Models\Subuser;
interface SubuserRepositoryInterface extends RepositoryInterface
{
/**
* Return a subuser with the associated server relationship.
*/
public function loadServerAndUserRelations(Subuser $subuser, bool $refresh = false): Subuser;
/**
* Return a subuser with the associated permissions relationship.
*/
public function getWithPermissions(Subuser $subuser, bool $refresh = false): Subuser;
/**
* Return a subuser and associated permissions given a user_id and server_id.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithPermissionsUsingUserAndServer(int $user, int $server): Subuser;
}

View File

@ -12,7 +12,6 @@ use App\Http\Controllers\Controller;
use App\Services\Eggs\Variables\VariableUpdateService;
use App\Http\Requests\Admin\Egg\EggVariableFormRequest;
use App\Services\Eggs\Variables\VariableCreationService;
use App\Contracts\Repository\EggVariableRepositoryInterface;
class EggVariableController extends Controller
{
@ -23,7 +22,6 @@ class EggVariableController extends Controller
protected AlertsMessageBag $alert,
protected VariableCreationService $creationService,
protected VariableUpdateService $updateService,
protected EggVariableRepositoryInterface $variableRepository,
protected ViewFactory $view
) {
}
@ -76,7 +74,7 @@ class EggVariableController extends Controller
*/
public function destroy(int $egg, EggVariable $variable): RedirectResponse
{
$this->variableRepository->delete($variable->id);
$variable->delete();
$this->alert->success(trans('admin/eggs.variables.notices.variable_deleted', [
'variable' => $variable->name,
]))->flash();

View File

@ -8,7 +8,6 @@ use Illuminate\Http\JsonResponse;
use App\Facades\Activity;
use App\Models\Permission;
use Illuminate\Support\Facades\Log;
use App\Repositories\Eloquent\SubuserRepository;
use App\Services\Subusers\SubuserCreationService;
use App\Repositories\Daemon\DaemonServerRepository;
use App\Transformers\Api\Client\SubuserTransformer;
@ -25,7 +24,6 @@ class SubuserController extends ClientApiController
* SubuserController constructor.
*/
public function __construct(
private SubuserRepository $repository,
private SubuserCreationService $creationService,
private DaemonServerRepository $serverRepository
) {
@ -110,9 +108,7 @@ class SubuserController extends ClientApiController
// have actually changed for the user.
if ($permissions !== $current) {
$log->transaction(function ($instance) use ($request, $subuser, $server) {
$this->repository->update($subuser->id, [
'permissions' => $this->getDefaultPermissions($request),
]);
$subuser->update(['permissions' => $this->getDefaultPermissions($request)]);
try {
$this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id);

View File

@ -3,11 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\Eloquent\SubuserRepository;
use App\Repositories\Eloquent\EggVariableRepository;
use App\Repositories\Eloquent\DatabaseHostRepository;
use App\Contracts\Repository\SubuserRepositoryInterface;
use App\Contracts\Repository\EggVariableRepositoryInterface;
use App\Contracts\Repository\DatabaseHostRepositoryInterface;
class RepositoryServiceProvider extends ServiceProvider
@ -19,7 +15,5 @@ class RepositoryServiceProvider extends ServiceProvider
{
// Eloquent Repositories
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
$this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class);
$this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class);
}
}

View File

@ -1,31 +0,0 @@
<?php
namespace App\Repositories\Eloquent;
use Illuminate\Support\Collection;
use App\Models\EggVariable;
use App\Contracts\Repository\EggVariableRepositoryInterface;
class EggVariableRepository extends EloquentRepository implements EggVariableRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return EggVariable::class;
}
/**
* Return editable variables for a given egg. Editable variables must be set to
* user viewable in order to be picked up by this function.
*/
public function getEditableVariables(int $egg): Collection
{
return $this->getBuilder()->where([
['egg_id', '=', $egg],
['user_viewable', '=', 1],
['user_editable', '=', 1],
])->get($this->getColumns());
}
}

View File

@ -1,69 +0,0 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Subuser;
use App\Exceptions\Repository\RecordNotFoundException;
use App\Contracts\Repository\SubuserRepositoryInterface;
class SubuserRepository extends EloquentRepository implements SubuserRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Subuser::class;
}
/**
* Return a subuser with the associated server relationship.
*/
public function loadServerAndUserRelations(Subuser $subuser, bool $refresh = false): Subuser
{
if (!$subuser->relationLoaded('server') || $refresh) {
$subuser->load('server');
}
if (!$subuser->relationLoaded('user') || $refresh) {
$subuser->load('user');
}
return $subuser;
}
/**
* Return a subuser with the associated permissions relationship.
*/
public function getWithPermissions(Subuser $subuser, bool $refresh = false): Subuser
{
if (!$subuser->relationLoaded('permissions') || $refresh) {
$subuser->load('permissions');
}
if (!$subuser->relationLoaded('user') || $refresh) {
$subuser->load('user');
}
return $subuser;
}
/**
* Return a subuser and associated permissions given a user_id and server_id.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getWithPermissionsUsingUserAndServer(int $user, int $server): Subuser
{
$instance = $this->getBuilder()->with('permissions')->where([
['user_id', '=', $user],
['server_id', '=', $server],
])->first();
if (is_null($instance)) {
throw new RecordNotFoundException();
}
return $instance;
}
}

View File

@ -5,7 +5,6 @@ namespace App\Services\Eggs\Variables;
use App\Models\EggVariable;
use App\Traits\Services\ValidatesValidationRules;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use App\Contracts\Repository\EggVariableRepositoryInterface;
use App\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableCreationService
@ -15,7 +14,7 @@ class VariableCreationService
/**
* VariableCreationService constructor.
*/
public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)
public function __construct(private ValidationFactory $validator)
{
}
@ -47,7 +46,8 @@ class VariableCreationService
$options = array_get($data, 'options') ?? [];
return $this->repository->create([
/** @var EggVariable $eggVariable */
$eggVariable = EggVariable::query()->create([
'egg_id' => $egg,
'name' => $data['name'] ?? '',
'description' => $data['description'] ?? '',
@ -57,5 +57,7 @@ class VariableCreationService
'user_editable' => in_array('user_editable', $options),
'rules' => $data['rules'] ?? '',
]);
return $eggVariable;
}
}

View File

@ -7,7 +7,6 @@ use App\Models\EggVariable;
use App\Exceptions\DisplayException;
use App\Traits\Services\ValidatesValidationRules;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use App\Contracts\Repository\EggVariableRepositoryInterface;
use App\Exceptions\Service\Egg\Variable\ReservedVariableNameException;
class VariableUpdateService
@ -17,7 +16,7 @@ class VariableUpdateService
/**
* VariableUpdateService constructor.
*/
public function __construct(private EggVariableRepositoryInterface $repository, private ValidationFactory $validator)
public function __construct(private ValidationFactory $validator)
{
}
@ -45,11 +44,11 @@ class VariableUpdateService
throw new ReservedVariableNameException(trans('exceptions.service.variables.reserved_name', ['name' => array_get($data, 'env_variable')]));
}
$search = $this->repository->setColumns('id')->findCountWhere([
['env_variable', '=', $data['env_variable']],
['egg_id', '=', $variable->egg_id],
['id', '!=', $variable->id],
]);
$search = EggVariable::query()
->where('env_variable', $data['env_variable'])
->where('egg_id', $variable->egg_id)
->whereNot('id', $variable->id)
->count();
if ($search > 0) {
throw new DisplayException(trans('exceptions.service.variables.env_not_unique', ['name' => array_get($data, 'env_variable')]));
@ -66,7 +65,7 @@ class VariableUpdateService
$options = array_get($data, 'options') ?? [];
return $this->repository->withoutFreshModel()->update($variable->id, [
return $variable->update([
'name' => $data['name'] ?? '',
'description' => $data['description'] ?? '',
'env_variable' => $data['env_variable'] ?? '',

View File

@ -8,7 +8,6 @@ use App\Models\Server;
use App\Models\Subuser;
use Illuminate\Database\ConnectionInterface;
use App\Services\Users\UserCreationService;
use App\Repositories\Eloquent\SubuserRepository;
use App\Exceptions\Service\Subuser\UserIsServerOwnerException;
use App\Exceptions\Service\Subuser\ServerSubuserExistsException;
@ -19,7 +18,6 @@ class SubuserCreationService
*/
public function __construct(
private ConnectionInterface $connection,
private SubuserRepository $subuserRepository,
private UserCreationService $userCreationService,
) {
}
@ -56,12 +54,12 @@ class SubuserCreationService
throw new UserIsServerOwnerException(trans('exceptions.subusers.user_is_owner'));
}
$subuserCount = $this->subuserRepository->findCountWhere([['user_id', '=', $user->id], ['server_id', '=', $server->id]]);
$subuserCount = $server->subusers()->where('user_id', $user->id)->count();
if ($subuserCount !== 0) {
throw new ServerSubuserExistsException(trans('exceptions.subusers.subuser_exists'));
}
return $this->subuserRepository->create([
return Subuser::query()->create([
'user_id' => $user->id,
'server_id' => $server->id,
'permissions' => array_unique($permissions),