Exemple #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);
 }
Exemple #2
0
 /**
  * @param Request $request
  * @param integer $movement
  *
  * @return RedirectResponse
  */
 protected function move(Request $request, $movement)
 {
     $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
     $resource = $this->findOr404($configuration);
     $position = $configuration->getSortablePosition();
     $accessor = PropertyAccess::createPropertyAccessor();
     $accessor->setValue($resource, $position, $accessor->getValue($resource, $position) + $movement);
     if (!$configuration->isHtmlRequest()) {
         return $this->viewHandler->handle($configuration, View::create($resource, 204));
     }
     $this->flashHelper->addSuccessFlash($configuration, 'move', $resource);
     return $this->redirectHandler->redirectToIndex($configuration, $resource);
 }
 /**
  * @param Request $request
  * @param int $movement
  *
  * @return RedirectResponse
  */
 protected function move(Request $request, $movement)
 {
     $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
     $resource = $this->findOr404($configuration);
     $position = $configuration->getSortablePosition();
     $accessor = PropertyAccess::createPropertyAccessor();
     $accessor->setValue($resource, $position, $accessor->getValue($resource, $position) + $movement);
     $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
     $this->manager->flush();
     $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
     if (!$configuration->isHtmlRequest()) {
         return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
     }
     $this->flashHelper->addSuccessFlash($configuration, 'move', $resource);
     return $this->redirectHandler->redirectToIndex($configuration, $resource);
 }
 /**
  * @param Request $request
  * @param string $transition
  * @param string $graph
  *
  * @return RedirectResponse
  */
 public function updateStateAction(Request $request, $transition, $graph)
 {
     $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
     $resource = $this->findOr404($configuration);
     $stateMachine = $this->get('sm.factory')->get($resource, $graph);
     if (!$stateMachine->can($transition)) {
         throw new NotFoundHttpException(sprintf('The requested transition %s cannot be applied on the given %s with graph %s.', $transition, $this->metadata->getName(), $graph));
     }
     $stateMachine->apply($transition);
     $this->manager->flush();
     if (!$configuration->isHtmlRequest()) {
         return $this->viewHandler->handle($configuration, View::create($resource, 204));
     }
     $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
     return $this->redirectHandler->redirectToReferer($configuration);
 }
 function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(MetadataInterface $metadata, RequestConfigurationFactoryInterface $requestConfigurationFactory, RequestConfiguration $configuration, AuthorizationCheckerInterface $authorizationChecker, RepositoryInterface $repository, 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::DELETE)->willReturn('sylius.product.delete');
     $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
     $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
     $configuration->isHtmlRequest()->willReturn(false);
     $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
     $event->isStopped()->willReturn(true);
     $event->getMessage()->willReturn('Cannot delete this product.');
     $event->getErrorCode()->willReturn(500);
     $repository->remove($resource)->shouldNotBeCalled();
     $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
     $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
     $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
     $this->shouldThrow(new HttpException(500, 'Cannot delete this product.'))->during('deleteAction', array($request));
 }