File: /home/u756937133/domains/swingersnest.com/public_html/app/Services/CoinRewardService.php
<?php
namespace App\Services;
use App\Models\SiteSetting;
use App\Models\User;
use App\Models\UserProfile;
use App\Models\UserCoinTransaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CoinRewardService
{
public const COINS_PER_FIELD_DEFAULT = 10;
/**
* Get the number of SNC coins to award per profile field (from admin settings).
*/
public static function getCoinsPerField(): int
{
$settings = SiteSetting::getSettings();
$value = $settings->snc_coins_per_field ?? null;
return $value !== null && $value >= 0 ? (int) $value : self::COINS_PER_FIELD_DEFAULT;
}
/**
* Grant SNC to user for filling a profile/account field, if not already rewarded for this source.
* Returns the amount granted.
*/
public function grantForField(User $user, string $source, ?string $description = null, ?int $amount = null): int
{
$amount = $amount ?? self::getCoinsPerField();
if ($amount <= 0) {
return 0;
}
$exists = UserCoinTransaction::where('user_id', $user->id)
->where('source', $source)
->exists();
if ($exists) {
return 0;
}
DB::transaction(function () use ($user, $source, $description, $amount) {
UserCoinTransaction::create([
'user_id' => $user->id,
'amount' => $amount,
'source' => $source,
'description' => $description ?? "Profile field: {$source}",
]);
$user->increment('snc_coins', $amount);
});
return $amount;
}
/**
* Check if a value is "filled" for reward purposes (non-empty).
*/
public static function isFilled($value): bool
{
if ($value === null) {
return false;
}
if (is_array($value)) {
return !empty($value);
}
if (is_string($value)) {
return trim($value) !== '';
}
return true;
}
/**
* Award SNC for any profile/account fields that were empty and are now filled.
* Call this before applying validated data to user/profile.
* Returns total coins awarded this request.
*/
public function grantProfileUpdateRewards(
User $user,
?UserProfile $profile,
array $validated,
Request $request,
bool $isCouple
): int {
$total = 0;
// User (account) fields
$userFields = [
'name' => $user->name,
'username' => $user->username,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'phone' => $user->phone,
'gender' => $user->gender,
'company' => $user->company,
'website_url' => $user->website_url,
'address' => $user->address,
'business_address' => $user->business_address,
];
foreach ($userFields as $field => $oldValue) {
$newValue = $validated[$field] ?? null;
if (!self::isFilled($oldValue) && self::isFilled($newValue)) {
$total += $this->grantForField($user, "user:{$field}");
}
}
// Profile image (user + profile)
$oldImage = $user->profile_image ?? ($profile->profile_photo ?? null);
$newImageFilled = $request->hasFile('profile_image') || $request->hasFile('profile_photo');
if (!self::isFilled($oldImage) && $newImageFilled) {
$total += $this->grantForField($user, 'user:profile_image');
}
if (!$profile) {
return $total;
}
// Profile fields (single)
$profileFields = [
'category', 'home_location', 'country', 'city', 'latitude', 'longitude',
'date_of_birth', 'sexuality', 'relationship_status', 'relationship_orientation',
'experience', 'smoking', 'looks_important', 'intelligence_important', 'travel_options',
'weight', 'height', 'body_type', 'eye_color', 'hair_color', 'tattoos', 'piercings',
'boob_size', 'dick_size', 'bio', 'looking_for', 'additional_notes',
];
foreach ($profileFields as $field) {
$oldValue = $profile->getAttribute($field);
$newValue = $validated[$field] ?? null;
if (!self::isFilled($oldValue) && self::isFilled($newValue)) {
$total += $this->grantForField($user, "profile:{$field}");
}
}
// JSON fields
$oldPrefs = $profile->preferences;
$newPrefs = $validated['preferences'] ?? null;
if (!self::isFilled($oldPrefs) && self::isFilled($newPrefs)) {
$total += $this->grantForField($user, 'profile:preferences');
}
$oldLangs = $profile->languages;
$newLangs = $validated['languages'] ?? null;
if (!self::isFilled($oldLangs) && self::isFilled($newLangs)) {
$total += $this->grantForField($user, 'profile:languages');
}
// Couple data fields
if ($isCouple) {
$existingCouple = $profile->couple_data
? (is_array($profile->couple_data) ? $profile->couple_data : json_decode($profile->couple_data, true) ?? [])
: [];
$coupleFields = [
'date_of_birth_her', 'date_of_birth_him', 'sexuality_her', 'sexuality_him',
'relationship_status_her', 'relationship_status_him', 'smoking_her', 'smoking_him',
'experience_her', 'experience_him', 'travel_options_her', 'travel_options_him',
'bio_her', 'bio_him', 'weight_her', 'weight_him', 'height_her', 'height_him',
'body_type_her', 'body_type_him', 'eye_color_her', 'eye_color_him',
'hair_color_her', 'hair_color_him', 'tattoos_her', 'tattoos_him',
'piercings_her', 'piercings_him', 'boob_size_her', 'dick_size_him',
];
foreach ($coupleFields as $field) {
$oldVal = $existingCouple[$field] ?? null;
$newVal = $validated[$field] ?? null;
if (!self::isFilled($oldVal) && self::isFilled($newVal)) {
$total += $this->grantForField($user, "profile:couple:{$field}");
}
}
}
return $total;
}
}