update rm deadcode
This commit is contained in:
@@ -6,6 +6,7 @@ class ProductCategoryData
|
||||
{
|
||||
public function __construct(
|
||||
public readonly int $legacyId,
|
||||
public readonly ?int $adoptId,
|
||||
public readonly string $name,
|
||||
public readonly string $slug,
|
||||
public readonly ?string $type,
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace App\Integrations\Whmcs\Support;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** Slugifies a candidate and appends -N until it's unique in the given column, excluding the row's own legacy_id. */
|
||||
class SlugUniquifier
|
||||
{
|
||||
public function unique(string $table, string $column, string $candidate, int $legacyId): string
|
||||
@@ -14,10 +13,27 @@ class SlugUniquifier
|
||||
$slug = $base;
|
||||
$suffix = 1;
|
||||
|
||||
while (DB::table($table)->where($column, $slug)->where('legacy_id', '!=', $legacyId)->exists()) {
|
||||
while ($this->ownedByAnotherRow($table, $column, $slug, $legacyId)) {
|
||||
$slug = "{$base}-" . ++$suffix;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
public function adoptableId(string $table, string $column, string $candidate): ?int
|
||||
{
|
||||
$slug = Str::slug($candidate) ?: 'item';
|
||||
|
||||
return DB::table($table)->where($column, $slug)->whereNull('legacy_id')->value('id');
|
||||
}
|
||||
|
||||
private function ownedByAnotherRow(string $table, string $column, string $slug, int $legacyId): bool
|
||||
{
|
||||
return DB::table($table)
|
||||
->where($column, $slug)
|
||||
->where(function ($query) use ($legacyId) {
|
||||
$query->whereNull('legacy_id')->orWhere('legacy_id', '!=', $legacyId);
|
||||
})
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,17 @@ class ProductCategoryTransformer
|
||||
$row = (array) $row;
|
||||
$legacyId = (int) $row['id'];
|
||||
$name = $row['name'] ?: "WHMCS Group {$legacyId}";
|
||||
$candidate = $row['slug'] ?: $name;
|
||||
|
||||
$adoptId = $this->slugs->adoptableId('product_categories', 'slug', $candidate);
|
||||
|
||||
return new ProductCategoryData(
|
||||
legacyId: $legacyId,
|
||||
adoptId: $adoptId,
|
||||
name: $name,
|
||||
slug: $this->slugs->unique('product_categories', 'slug', $row['slug'] ?: $name, $legacyId),
|
||||
slug: $adoptId
|
||||
? (Str::slug($candidate) ?: 'item')
|
||||
: $this->slugs->unique('product_categories', 'slug', $candidate, $legacyId),
|
||||
type: $this->guessType($name),
|
||||
isActive: ! $row['hidden'],
|
||||
sortOrder: (int) $row['order'],
|
||||
|
||||
@@ -10,8 +10,9 @@ class ProductCategoryWriter
|
||||
public function write(ProductCategoryData $data): void
|
||||
{
|
||||
DB::table('product_categories')->updateOrInsert(
|
||||
['legacy_id' => $data->legacyId],
|
||||
$data->adoptId ? ['id' => $data->adoptId] : ['legacy_id' => $data->legacyId],
|
||||
[
|
||||
'legacy_id' => $data->legacyId,
|
||||
'name' => $data->name,
|
||||
'slug' => $data->slug,
|
||||
'type' => $data->type,
|
||||
|
||||
Reference in New Issue
Block a user