TDoG-Skin/app/Listeners/CleanUpCloset.php

35 lines
863 B
PHP
Raw Permalink Normal View History

2024-08-17 18:43:48 +08:00
<?php
namespace App\Listeners;
use App\Models\Texture;
use App\Models\User;
class CleanUpCloset
{
public function handle(Texture $texture)
{
// no need to update users' closet
// if texture was switched from "private" to "public"
if ($texture->exists && $texture->public) {
return;
}
$likers = $texture
->likers()
->where('user_uid', '!=', $texture->uploader)
->get();
$likers->each(function (User $user) use ($texture) {
$user->closet()->detach($texture->tid);
if (option('return_score')) {
$user->score += (int) option('score_per_closet_item');
$user->save();
}
});
if ($texture->exists) {
$texture->decrement('likes', $likers->count());
}
}
}