Ejemplo n.º 1
0
 /**
  * @param Request $request
  *
  * @return RedirectResponse
  */
 public function applyStateMachineTransitionAction(Request $request)
 {
     $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
     $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
     $resource = $this->findOr404($configuration);
     $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
     if ($event->isStopped() && !$configuration->isHtmlRequest()) {
         throw new HttpException($event->getErrorCode(), $event->getMessage());
     }
     if ($event->isStopped()) {
         $this->flashHelper->addFlashFromEvent($configuration, $event);
         return $this->redirectHandler->redirectToResource($configuration, $resource);
     }
     if (!$this->stateMachine->can($configuration, $resource)) {
         throw new BadRequestHttpException();
     }
     $this->stateMachine->apply($configuration, $resource);
     $this->manager->flush();
     $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
     if (!$configuration->isHtmlRequest()) {
         return $this->viewHandler->handle($configuration, View::create($resource, Response::HTTP_OK));
     }
     $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
     return $this->redirectHandler->redirectToResource($configuration, $resource);
 }
Ejemplo n.º 2
0
 function it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(MetadataInterface $metadata, RequestConfigurationFactoryInterface $requestConfigurationFactory, RequestConfiguration $configuration, AuthorizationCheckerInterface $authorizationChecker, RepositoryInterface $repository, ObjectManager $objectManager, StateMachineInterface $stateMachine, SingleResourceProviderInterface $singleResourceProvider, ResourceInterface $resource, FlashHelperInterface $flashHelper, EventDispatcherInterface $eventDispatcher, ResourceControllerEvent $event, Request $request)
 {
     $metadata->getApplicationName()->willReturn('sylius');
     $metadata->getName()->willReturn('product');
     $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
     $configuration->hasPermission()->willReturn(true);
     $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
     $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
     $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
     $configuration->isHtmlRequest()->willReturn(false);
     $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
     $event->isStopped()->willReturn(true);
     $event->getMessage()->willReturn('Cannot approve this product.');
     $event->getErrorCode()->willReturn(500);
     $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
     $objectManager->flush()->shouldNotBeCalled();
     $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
     $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
     $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
     $this->shouldThrow(new HttpException(500, 'Cannot approve this product.'))->during('applyStateMachineTransitionAction', [$request]);
 }