mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-20 02:11:36 +00:00
42 lines
872 B
PHP
42 lines
872 B
PHP
<?php
|
|
|
|
namespace App\Actions\Script;
|
|
|
|
use App\Models\Script;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class CreateScript
|
|
{
|
|
/**
|
|
* @throws ValidationException
|
|
*/
|
|
public function handle(User $creator, array $input): Script
|
|
{
|
|
$this->validateInputs($input);
|
|
|
|
$script = new Script([
|
|
'user_id' => $creator->id,
|
|
'name' => $input['name'],
|
|
'content' => $input['content'],
|
|
]);
|
|
$script->save();
|
|
|
|
return $script;
|
|
}
|
|
|
|
/**
|
|
* @throws ValidationException
|
|
*/
|
|
private function validateInputs(array $input): void
|
|
{
|
|
$rules = [
|
|
'name' => 'required',
|
|
'content' => 'required',
|
|
];
|
|
|
|
Validator::make($input, $rules)->validateWithBag('createScript');
|
|
}
|
|
}
|