2018-02-27 22:09:34 -06:00
< ? php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Client\Servers ;
2018-02-27 22:09:34 -06:00
2024-03-12 22:39:16 -04:00
use App\Facades\Activity ;
use App\Http\Controllers\Api\Client\ClientApiController ;
use App\Http\Requests\Api\Client\Servers\SendCommandRequest ;
2025-09-24 13:34:19 +02:00
use App\Models\Server ;
2025-02-26 16:12:19 +01:00
use Dedoc\Scramble\Attributes\Group ;
2025-01-07 22:58:04 +01:00
use Exception ;
2025-09-24 13:34:19 +02:00
use GuzzleHttp\Exception\BadResponseException ;
2025-01-07 22:58:04 +01:00
use Illuminate\Http\Client\ConnectionException ;
2025-09-24 13:34:19 +02:00
use Illuminate\Http\Response ;
use Symfony\Component\HttpKernel\Exception\HttpException ;
2018-02-27 22:09:34 -06:00
2025-02-26 16:12:19 +01:00
#[Group('Server', weight: 1)]
2018-02-27 22:09:34 -06:00
class CommandController extends ClientApiController
{
/**
2025-02-26 16:12:19 +01:00
* Send command
*
2018-02-27 22:09:34 -06:00
* Send a command to a running server .
*
2025-01-07 22:58:04 +01:00
* @ throws ConnectionException
2018-02-27 22:09:34 -06:00
*/
2019-09-05 21:31:12 -07:00
public function index ( SendCommandRequest $request , Server $server ) : Response
2018-02-27 22:09:34 -06:00
{
try {
2024-03-16 15:11:10 -04:00
$server -> send ( $request -> input ( 'command' ));
2025-01-07 22:58:04 +01:00
} catch ( Exception $exception ) {
2020-07-14 21:16:38 -07:00
$previous = $exception -> getPrevious ();
if ( $previous instanceof BadResponseException ) {
2026-07-09 09:55:11 -04:00
throw_if ( $previous -> getResponse () -> getStatusCode () === Response :: HTTP_BAD_GATEWAY , new HttpException ( Response :: HTTP_BAD_GATEWAY , 'Server must be online in order to send commands.' , $exception ));
2018-02-27 22:09:34 -06:00
}
2020-07-14 21:16:38 -07:00
throw $exception ;
2018-02-27 22:09:34 -06:00
}
2025-01-07 22:58:04 +01:00
Activity :: event ( 'server:console.command' )
-> property ( 'command' , $request -> input ( 'command' ))
-> log ();
2022-07-04 18:11:53 -04:00
2018-02-27 22:09:34 -06:00
return $this -> returnNoContent ();
}
}