42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\Core\MigrateImport\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||
|
|
|
||
|
|
class ImportMapping extends Model
|
||
|
|
{
|
||
|
|
protected $guarded = [];
|
||
|
|
|
||
|
|
public function model(): MorphTo
|
||
|
|
{
|
||
|
|
return $this->morphTo();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function resolve(string $source, string $sourceType, string $externalId): ?Model
|
||
|
|
{
|
||
|
|
return static::query()
|
||
|
|
->where('source', $source)
|
||
|
|
->where('source_type', $sourceType)
|
||
|
|
->where('external_id', $externalId)
|
||
|
|
->first()
|
||
|
|
?->model;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function record(string $source, string $sourceType, string $externalId, Model $model): self
|
||
|
|
{
|
||
|
|
return static::query()->updateOrCreate(
|
||
|
|
[
|
||
|
|
'source' => $source,
|
||
|
|
'source_type' => $sourceType,
|
||
|
|
'external_id' => $externalId,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'model_type' => $model->getMorphClass(),
|
||
|
|
'model_id' => $model->getKey(),
|
||
|
|
],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|