<?php namespace Eform\Instruction\Actions; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Litepie\Actions\Concerns\AsAction; use Eform\Instruction\Models\Instructions; class InstructionsAction { use AsAction; protected $model; protected $namespace = 'eform.instruction.instructions'; protected $eventClass = \Eform\Instruction\Events\InstructionsAction::class; protected $action; protected $function; protected $request; public function handle(string $action, Instructions $instructions, array $request = []) { $this->action = $action; $this->request = $request; $this->model = $instructions; $this->function = Str::camel($action); $this->executeAction(); return $this->model; } public function store(Instructions $instructions, array $request) { $attributes = $request; $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $instructions = $instructions->create($attributes); return $instructions; } public function update(Instructions $instructions, array $request) { $attributes = $request; $instructions->update($attributes); return $instructions; } public function destroy(Instructions $instructions, array $request) { $instructions->delete(); return $instructions; } public function copy(Instructions $instructions, array $request) { $count = $request['count'] ?: 1; if ($count == 1) { $instructions = $instructions->replicate(); $instructions->created_at = Carbon::now(); $instructions->save(); return $instructions; } for ($i = 1; $i <= $count; $i++) { $new = $instructions->replicate(); $new->created_at = Carbon::now(); $new->save(); } return $instructions; } }