Skip to content

Commit

Permalink
Use handle instead of toggle & use const isnstead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
RMartinOscar committed Jan 6, 2025
1 parent 2525af8 commit b5bcdc7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ public function form(Form $form): Form
->color('warning')
->hidden(fn (Server $server) => $server->isSuspended())
->action(function (SuspensionService $suspensionService, Server $server) {
$suspensionService->toggle($server, 'suspend');
$suspensionService->handle($server, SuspensionService::ACTION_SUSPEND);
Notification::make()->success()->title('Server Suspended!')->send();

$this->refreshFormData(['status', 'docker']);
Expand All @@ -802,7 +802,7 @@ public function form(Form $form): Form
->color('success')
->hidden(fn (Server $server) => !$server->isSuspended())
->action(function (SuspensionService $suspensionService, Server $server) {
$suspensionService->toggle($server, 'unsuspend');
$suspensionService->handle($server, SuspensionService::ACTION_UNSUSPEND);
Notification::make()->success()->title('Server Unsuspended!')->send();

$this->refreshFormData(['status', 'docker']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function table(Table $table): Table
->color('warning')
->action(function (SuspensionService $suspensionService) use ($user) {
foreach ($user->servers()->whereNot('status', ServerState::Suspended)->get() as $server) {
$suspensionService->toggle($server);
$suspensionService->handle($server);
}
}),
Actions\Action::make('toggleUnsuspend')
Expand All @@ -43,7 +43,7 @@ public function table(Table $table): Table
->color('primary')
->action(function (SuspensionService $suspensionService) use ($user) {
foreach ($user->servers()->where('status', ServerState::Suspended)->get() as $server) {
$suspensionService->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$suspensionService->handle($server, SuspensionService::ACTION_UNSUSPEND);
}
}),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function suspend(ServerWriteRequest $request, Server $server): Response
{
$this->suspensionService->toggle($server);
$this->suspensionService->handle($server);

return $this->returnNoContent();
}
Expand All @@ -44,7 +44,7 @@ public function suspend(ServerWriteRequest $request, Server $server): Response
*/
public function unsuspend(ServerWriteRequest $request, Server $server): Response
{
$this->suspensionService->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$this->suspensionService->handle($server, SuspensionService::ACTION_UNSUSPEND);

return $this->returnNoContent();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Servers/SuspensionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
*
* @throws \Throwable
*/
public function toggle(Server $server, string $action = self::ACTION_SUSPEND): void
public function handle(Server $server, string $action = self::ACTION_SUSPEND): void
{
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);

Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Services/Servers/SuspensionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function testServerIsSuspendedAndUnsuspended(): void

$this->repository->expects('setServer->sync')->twice()->andReturnSelf();

$this->getService()->toggle($server);
$this->getService()->handle($server);

$this->assertTrue($server->refresh()->isSuspended());

$this->getService()->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$this->getService()->handle($server, SuspensionService::ACTION_UNSUSPEND);

$this->assertFalse($server->refresh()->isSuspended());
}
Expand All @@ -42,13 +42,13 @@ public function testNoActionIsTakenIfSuspensionStatusIsUnchanged(): void
{
$server = $this->createServerModel();

$this->getService()->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$this->getService()->handle($server, SuspensionService::ACTION_UNSUSPEND);

$server->refresh();
$this->assertFalse($server->isSuspended());

$server->update(['status' => ServerState::Suspended]);
$this->getService()->toggle($server);
$this->getService()->handle($server);

$server->refresh();
$this->assertTrue($server->isSuspended());
Expand All @@ -61,7 +61,7 @@ public function testExceptionIsThrownIfInvalidActionsArePassed(): void
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected one of: "suspend", "unsuspend". Got: "foo"');

$this->getService()->toggle($server, 'foo');
$this->getService()->handle($server, 'foo');
}

private function getService(): SuspensionService
Expand Down

0 comments on commit b5bcdc7

Please sign in to comment.