66 lines
2.3 KiB
PHP
66 lines
2.3 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Services\Site;
|
|
|
|
use App\Dto\Service\Site\Feedback\Send;
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use App\Notifications\ReviewAdded;
|
|
use App\ServiceResults\ServiceResultError;
|
|
use App\ServiceResults\ServiceResultSuccess;
|
|
use App\Services\ProjectFeedback\ProjectFeedbackCommandHandler;
|
|
use App\Services\Service;
|
|
use App\Services\WebsiteTranslations;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
final class FeedbackService extends Service
|
|
{
|
|
public function __construct(
|
|
private readonly ProjectFeedbackCommandHandler $feedbackCommandHandler,
|
|
private readonly bool $isNotifications = false,
|
|
private readonly ?string $mailNotifications = null,
|
|
) { }
|
|
|
|
public function send(Send $send, Project $project, WebsiteTranslations $websiteTranslations, ?User $user = null): ServiceResultError | ServiceResultSuccess
|
|
{
|
|
try {
|
|
$feedback = DB::transaction(function () use ($send, $project, $user) {
|
|
$data = $this->getDataFeedback($send);
|
|
$data['user_id'] = $user?->id;
|
|
return $this->feedbackCommandHandler->handleStore($project, $data);
|
|
});
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
return $this->errService($websiteTranslations->translate('Server Error'));
|
|
}
|
|
|
|
try {
|
|
if ($this->isNotifications) {
|
|
Notification::route('mail', $this->mailNotifications)->notify(new ReviewAdded($feedback));
|
|
}
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
}
|
|
|
|
return $this->ok($websiteTranslations->translate('site.Message sent successfully'));
|
|
}
|
|
|
|
private function getDataFeedback(Send $send): array
|
|
{
|
|
$name = $send->getName();
|
|
if ($name !== null) {
|
|
$name = strip_tags($name);
|
|
}
|
|
$message = $send->getMessage();
|
|
$message = strip_tags($message, ['<b>', '<strong>', '<p>', '<br>', '<ul>', '<li>', '<ol>', '<table>', '<tr>', '<td>', '<th>', '<i>',]);
|
|
return [
|
|
'name' => $name,
|
|
'email' => $send->getEmail(),
|
|
'message' => $message,
|
|
'ip' => $send->getHttpUserData()->getClientIp(),
|
|
'user_agent' => $send->getHttpUserData()->getUserAgent(),
|
|
];
|
|
}
|
|
}
|