57 خطوط
1.5 KiB
PHP
57 خطوط
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\Call;
|
|
use App\Models\Campaign;
|
|
use App\Models\Company;
|
|
use App\Models\Contact;
|
|
use App\Models\Deal;
|
|
use App\Models\Lead;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
final class EntityResolver
|
|
{
|
|
public static function resolve(string $type, int $id): Model
|
|
{
|
|
$class = self::classFor($type);
|
|
|
|
return $class::findOrFail($id);
|
|
}
|
|
|
|
public static function classFor(string $type): string
|
|
{
|
|
return match ($type) {
|
|
'lead' => Lead::class,
|
|
'company' => Company::class,
|
|
'deal' => Deal::class,
|
|
'contact' => Contact::class,
|
|
'call' => Call::class,
|
|
'campaign' => Campaign::class,
|
|
default => abort(422, 'نوع موجودیت پشتیبانی نمیشود.'),
|
|
};
|
|
}
|
|
|
|
public static function typeOf(Model $entity): string
|
|
{
|
|
return match (true) {
|
|
$entity instanceof Lead => 'lead',
|
|
$entity instanceof Company => 'company',
|
|
$entity instanceof Deal => 'deal',
|
|
$entity instanceof Contact => 'contact',
|
|
$entity instanceof Call => 'call',
|
|
$entity instanceof Campaign => 'campaign',
|
|
default => 'unknown',
|
|
};
|
|
}
|
|
|
|
public static function authorize(string $type, int $id, string $ability = 'view'): Model
|
|
{
|
|
$entity = self::resolve($type, $id);
|
|
Gate::authorize($ability, $entity);
|
|
|
|
return $entity;
|
|
}
|
|
}
|