source user id => customer id */ private static array $cache = []; public function resolve(int $sourceUserId): ?int { if (array_key_exists($sourceUserId, self::$cache)) { return self::$cache[$sourceUserId]; } $user = DB::connection('domain')->table('users')->where('id', $sourceUserId)->first(); if (! $user) { return self::$cache[$sourceUserId] = null; } $email = strtolower($user->email); $customer = DB::table('customers')->where('email', $email)->first(); if ($customer) { DB::table('customers')->where('id', $customer->id)->update([ 'username' => $customer->username ?? $user->username, 'type' => $user->type, ]); return self::$cache[$sourceUserId] = $customer->id; } $customerId = DB::table('customers')->insertGetId([ 'uuid' => (string) Str::uuid(), 'fullname' => $user->name, 'username' => $user->username, 'email' => $email, 'email_verified_at' => $user->email_verified_at, // source hashes are Laravel bcrypt (self-contained, no APP_KEY needed), safe to copy as-is 'password' => $user->password, 'type' => $user->type, 'status' => 1, 'created_at' => $user->created_at, 'updated_at' => $user->updated_at, ]); return self::$cache[$sourceUserId] = $customerId; } }