siakad-itm/app/Services/Admin/FeedbackService.php

99 lines
3.0 KiB
PHP

<?php
namespace App\Services\Admin;
use App\Enums\FeedbackStatus;
use App\Models\Feedback;
use App\Models\User;
use App\Support\TextAlignAttributeSanitizer;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
class FeedbackService
{
private readonly HtmlSanitizer $sanitizer;
public function __construct()
{
$config = (new HtmlSanitizerConfig)
->allowElement('p', ['style'])
->allowElement('h2', ['style'])
->allowElement('h3', ['style'])
->allowElement('strong')
->allowElement('b')
->allowElement('em')
->allowElement('i')
->allowElement('u')
->allowElement('s')
->allowElement('strike')
->allowElement('ul')
->allowElement('ol')
->allowElement('li')
->allowElement('blockquote')
->allowElement('br')
->allowElement('a', ['href'])
->allowElement('img', ['src', 'alt'])
->allowLinkSchemes(['http', 'https', 'mailto'])
->allowMediaSchemes(['http', 'https'])
->withAttributeSanitizer(new TextAlignAttributeSanitizer);
$this->sanitizer = new HtmlSanitizer($config);
}
public function pendingCount(User $user): int
{
return Feedback::query()
->where('user_id', $user->id)
->where('status', '!=', FeedbackStatus::Resolved)
->count();
}
public function paginated(User $user, int $perPage = 25, string $search = '', ?string $type = null, ?string $status = null): LengthAwarePaginator
{
return Feedback::query()
->where('user_id', $user->id)
->with(['user.profile', 'handler.profile'])
->when($search, fn ($q) => $q->where('subject', 'like', "%{$search}%"))
->when($type, fn ($q) => $q->where('type', $type))
->when($status, fn ($q) => $q->where('status', $status))
->latest()
->paginate($perPage);
}
public function create(User $user, array $data): Feedback
{
return Feedback::create([
'user_id' => $user->id,
'type' => $data['type'],
'subject' => $data['subject'],
'message' => $this->sanitizer->sanitize($data['message']),
]);
}
public function update(Feedback $feedback, array $data): Feedback
{
$feedback->type = $data['type'];
$feedback->subject = $data['subject'];
$feedback->message = $this->sanitizer->sanitize($data['message']);
$feedback->update();
return $feedback;
}
public function updateStatus(Feedback $feedback, string $status): Feedback
{
$feedback->update([
'status' => $status,
'handled_by' => auth()->id(),
]);
return $feedback;
}
public function delete(Feedback $feedback): bool
{
return $feedback->delete();
}
}