chg: dev: refactor the SecurityController #7
This commit is contained in:
71
src/Service/Email/SendActivationEmailService.php
Normal file
71
src/Service/Email/SendActivationEmailService.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Service\Email;
|
||||
|
||||
use App\Entity\User;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
|
||||
/**
|
||||
* Class SendActivationEmailService
|
||||
*
|
||||
* @package App\Service\Email
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 20.
|
||||
*/
|
||||
readonly final class SendActivationEmailService
|
||||
{
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
private MailerInterface $mailer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function send(User $user, string $activationUrl): void
|
||||
{
|
||||
try {
|
||||
$this->mailer->send(
|
||||
new TemplatedEmail()
|
||||
->from('noreply@mineseeker.hu')
|
||||
->to($user->email)
|
||||
->subject('Activate your MineSeeker account')
|
||||
->htmlTemplate('emails/activation.html.twig')
|
||||
->context([
|
||||
'username' => $user->getUsername(),
|
||||
'activation_url' => $activationUrl,
|
||||
])
|
||||
);
|
||||
} catch (TransportExceptionInterface|Exception $e) {
|
||||
$this->logger->error("Failed to send activation email: {$e->getMessage()}", [
|
||||
'exception' => $e,
|
||||
'user' => $user->getUsername(),
|
||||
]);
|
||||
throw new RuntimeException("Failed to send activation email: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
70
src/Service/Email/SendPasswordResetEmailService.php
Normal file
70
src/Service/Email/SendPasswordResetEmailService.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Service\Email;
|
||||
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
|
||||
/**
|
||||
* Class SendPasswordResetEmailService
|
||||
*
|
||||
* @package App\Service\Email
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 20.
|
||||
*/
|
||||
readonly final class SendPasswordResetEmailService
|
||||
{
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
private MailerInterface $mailer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function send(string $email, string $username, string $resetUrl): void
|
||||
{
|
||||
try {
|
||||
$this->mailer->send(
|
||||
new TemplatedEmail()
|
||||
->from('noreply@mineseeker.hu')
|
||||
->to($email)
|
||||
->subject('Reset your MineSeeker password')
|
||||
->htmlTemplate('emails/reset_password.html.twig')
|
||||
->context([
|
||||
'username' => $username,
|
||||
'reset_url' => $resetUrl,
|
||||
])
|
||||
);
|
||||
} catch (TransportExceptionInterface|Exception $e) {
|
||||
$this->logger->error("Failed to send password reset email: {$e->getMessage()}", [
|
||||
'exception' => $e,
|
||||
'email' => $email,
|
||||
]);
|
||||
throw new RuntimeException("Failed to send password reset email: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
75
src/Service/Email/SendUserActivationNotificationService.php
Normal file
75
src/Service/Email/SendUserActivationNotificationService.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Service\Email;
|
||||
|
||||
use App\Entity\User;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
|
||||
/**
|
||||
* Class SendUserActivationNotificationService
|
||||
*
|
||||
* @package App\Service\Email
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 20.
|
||||
*/
|
||||
readonly final class SendUserActivationNotificationService
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(env: 'APP_CONTACT_MAIL_ADDRESS')]
|
||||
private string $appContactMailAddress,
|
||||
private LoggerInterface $logger,
|
||||
private MailerInterface $mailer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function send(User $user, DateTime $activatedAt): void
|
||||
{
|
||||
try {
|
||||
$this->mailer->send(
|
||||
new TemplatedEmail()
|
||||
->from('noreply@mineseeker.hu')
|
||||
->to($this->appContactMailAddress)
|
||||
->subject("✅ User Account Activated: {$user->getUsername()}")
|
||||
->htmlTemplate('emails/user_activation_notification.html.twig')
|
||||
->context([
|
||||
'user' => $user,
|
||||
'activatedAt' => $activatedAt,
|
||||
])
|
||||
);
|
||||
} catch (TransportExceptionInterface|Exception $e) {
|
||||
$this->logger->error("Failed to send user activation notification: {$e->getMessage()}", [
|
||||
'exception' => $e,
|
||||
'user' => $user->getUsername(),
|
||||
]);
|
||||
throw new RuntimeException("Failed to send user activation notification: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Service\Email;
|
||||
|
||||
use App\Entity\User;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
|
||||
/**
|
||||
* Class SendUserRegistrationNotificationService
|
||||
*
|
||||
* @package App\Service\Email
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 20.
|
||||
*/
|
||||
readonly final class SendUserRegistrationNotificationService
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(env: 'APP_CONTACT_MAIL_ADDRESS')]
|
||||
private string $appContactMailAddress,
|
||||
private LoggerInterface $logger,
|
||||
private MailerInterface $mailer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function send(User $user, DateTime $registeredAt): void
|
||||
{
|
||||
try {
|
||||
$this->mailer->send(
|
||||
new TemplatedEmail()
|
||||
->from('noreply@mineseeker.hu')
|
||||
->to($this->appContactMailAddress)
|
||||
->subject("🎉 New User Registration: {$user->getUsername()}")
|
||||
->htmlTemplate('emails/user_registration_notification.html.twig')
|
||||
->context([
|
||||
'user' => $user,
|
||||
'registeredAt' => $registeredAt,
|
||||
])
|
||||
);
|
||||
} catch (TransportExceptionInterface|Exception $e) {
|
||||
$this->logger->error("Failed to send user registration notification: {$e->getMessage()}", [
|
||||
'exception' => $e,
|
||||
'user' => $user->getUsername(),
|
||||
]);
|
||||
throw new RuntimeException("Failed to send user registration notification: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user