Exemplo n.º 1
0
 /**
  * Транзакция с локированием добываемого ресурса
  * @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;
     }
 }