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