/** * change user's email or phone, with validation code received in previous step. * * @Annotations\Patch("/users/{id}/contact-info") */ public function patchUserChangeContactInfoAction(User $user, Request $request) { $requestData = $this->requestIsJsonWithKeysOrThrow($request, ['new_contact_info', 'validation_code']); if ($requestData['validation_code'] !== $user->getConfirmationToken()) { throw new BadRequestHttpException('wrong validation code'); } $contactInfo = $requestData['new_contact_info']; $manager = $this->get('fos_user.user_manager'); $validator = $this->container->get('validator'); $emailAssert = new Assert\Email(); $emailAssert->message = 'bst.email.invalid'; $errors = $validator->validateValue($contactInfo, $emailAssert); if (count($errors) === 0) { $this->get('logger')->info('updated email of ' . $user->getId() . ' with ' . $contactInfo); $user->setEmail($contactInfo); $manager->updateUser($user); return $this->handleView(new View()); } // we set user directly here so we can reuse the validator // of User entity for phone number $phoneNumber = str_replace('+', '00', $contactInfo); $user->setPhoneNumber($phoneNumber); $errors = $validator->validate($user, ['phone_check']); if (count($errors) === 0) { $this->get('logger')->info('updated phone of ' . $user->getId() . ' with ' . $phoneNumber); $manager->updateUser($user); return $this->handleView(new View()); } return $this->handleView(new View(['message' => 'bst.changecontactinfo.invalid'], Response::HTTP_BAD_REQUEST)); }