Skip to content

Commit c0c852b

Browse files
committed
Refactor GenerateUniqueShopKeyJob to generate unique shop keys directly within the handle method
This commit modifies the GenerateUniqueShopKeyJob to implement a new method for generating unique shop keys. The previous dependency on GenerateUniqueShopNameService has been removed, and the logic for creating a unique shop key is now encapsulated within the job itself, ensuring that the generated keys are unique by checking against existing entries in the database.
1 parent 1a34937 commit c0c852b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

app/Jobs/GenerateUniqueShopKeyJob.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Illuminate\Queue\InteractsWithQueue;
99
use Illuminate\Contracts\Queue\ShouldQueue;
1010
use Illuminate\Foundation\Bus\Dispatchable;
11-
use App\Services\Shop\GenerateUniqueShopNameService;
1211

1312
class GenerateUniqueShopKeyJob implements ShouldQueue
1413
{
@@ -30,7 +29,23 @@ public function __construct($shopId)
3029
public function handle(): void
3130
{
3231
$shop=Shop::find($this->shopId);
33-
$shop->shop_key=(new GenerateUniqueShopNameService())->generateUniqueShopName($shop->shop_name);
32+
33+
$shopKey = '';
34+
do {
35+
// Nettoyer et préparer le préfixe à partir du nom de la boutique
36+
$prefix = strtolower($shop->shop_name); // Conversion en minuscules
37+
$prefix = preg_replace('/[^a-z0-9]/', '', $prefix); // Supprime les caractères spéciaux
38+
$prefix = substr($prefix, 0, 4); // Prend les 4 premiers caractères
39+
$prefix .= '-'; // Ajoute le tiret
40+
41+
// Générer une chaîne aléatoire de 5 caractères
42+
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
43+
$randomString = substr(str_shuffle($characters), 0, 5);
44+
45+
// Combiner le préfixe avec la chaîne aléatoire
46+
$shopKey = $prefix . $randomString;
47+
} while (Shop::where('shop_key', $shopKey)->exists());
48+
$shop->shop_key=$shopKey;
3449
$shop->save();
3550
}
3651
}

0 commit comments

Comments
 (0)