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));
 }
Beispiel #2
0
 /**
  * @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, 204));
     }
     $this->flashHelper->addSuccessFlash($configuration, 'move', $resource);
     return $this->redirectHandler->redirectToIndex($configuration, $resource);
 }
Beispiel #3
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);
 }
Beispiel #4
0
 /**
  * @param Request $request
  * @param $enabled
  *
  * @return RedirectResponse
  */
 protected function toggle(Request $request, $enabled)
 {
     $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
     $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
     $resource = $this->findOr404($configuration);
     $resource->setEnabled($enabled);
     $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($resource, 204));
     }
     $this->flashHelper->addSuccessFlash($configuration, $enabled ? 'enable' : 'disable', $resource);
     return $this->redirectHandler->redirectToIndex($configuration, $resource);
 }