Answer the question
In order to leave comments, you need to log in
How to do it right from the point of view of OOP and common sense?
You need to activate your user account.
There is a user entity, with an activation method:
class User
{
private $is_active = false;
public function activate(string $activation_token) : void
{
if ($activation_token !== $this->getActivationToken()) {
throw new UserActivationFailException();
}
$this->is_active = true;
}
public function getActivationToken() : string
{
//генерация ключа активации
}
}
public function completeSignUpAction(Request $request) : Response
{
$email = mb_strtolower(trim($request->get('email')), 'UTF-8');
$activation_token = trim($request->get('token'));
//валидация
$entity_manager = $this->getDoctrine()->getManager();
try {
//ну или в контейнер можно положить
(new UserActivator($entity_manager))->activate($email, $activation_token);
} catch (UserActivationFailException $e) {
throw new NotFoundHttpException();
}
return new RedirectResponse($this->generateUrl('login'));
}
class UserActivator
{
private $entity_manager;
public function __construct(EntityManager $entity_manager)
{
$this->entity_manager = $entity_manager;
}
public function activate(string $email, string $activation_token) : void
{
//проверка входных данных
$user_repository = $this->entity_manager->getRepository(User::class);
$user = $user_repository->findOneBy(['email' => $email]);
if (is_null($user)) {
throw new UserActivationFailException();
}
$user->activate($activation_token);
$this->entity_manager->flush($user);
}
}
Answer the question
In order to leave comments, you need to log in
I won’t say for the symphony, but yes. Everything is exactly as you described. Inject repository dependencies into services, inject services into controllers, and work with services in controller actions. There can be a lot of different services.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question