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