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