private function updateUserAndForm(User &$user, FormInterface &$form, UserService $userService) { $cyrillicName = $userService->transliterate($user->getLiteralUsername()); if ($this->isAllreadyExists($cyrillicName)) { $errorMessage = $this->get('translator')->trans('fos_user.username.already_used', [], 'validators'); $form->addError(new FormError($errorMessage)); } else { $user->setName($cyrillicName); } }
/** * Транзакция с локированием добываемого ресурса * @param EntityManager $em * @param int $resourceId * @param UserService $userService * @return void * @throws \Doctrine\DBAL\ConnectionException * @throws \Exception */ private function reduceQuantity(EntityManager $em, int $resourceId, UserService $userService) { $em->getConnection()->beginTransaction(); try { /** @var RoomResource $resourceToUpdate */ $resourceToUpdate = $em->find(RoomResource::class, $resourceId, LockMode::PESSIMISTIC_READ); $resourceToUpdate->reduceQuantity($this->quantityToObtain); $userService->takeItems($this->user, $resourceToUpdate->getItem(), $this->quantityToObtain); $em->persist($resourceToUpdate); $em->flush(); $em->getConnection()->commit(); } catch (\Exception $e) { $em->getConnection()->rollback(); throw $e; } }