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