public function saveBatchActionsAction(Request $request, $id)
{
$user = $this->container->get('user')->getCurrentUser();
if (!$user->hasPermission('ManagePlaylist')) {
throw new AccessDeniedException('You do not have the right to manage playlists.');
}
$em = $this->container->get('em');
$urlMatcher = $this->container->get('router');
$controllerResolver = $this->container->get('controller_resolver');
$kernel = $this->container->get('kernel');
$dispatcher = $this->container->get('dispatcher');
$playlist = $em->getRepository('Newscoop\\Entity\\Playlist')->getPlaylist($id)->getOneOrNullResult();
if (!$playlist) {
throw new NotFoundHttpException('Result was not found.');
}
$actions = $request->request->get('actions', array());
$lastArtclesModificationTime = $request->request->get('articlesModificationTime');
if (!$lastArtclesModificationTime && $playlist->getArticlesModificationTime() != null) {
throw new InvalidParametersException('articlesModificationTime parameter is required');
} elseif (new \DateTime($lastArtclesModificationTime, new \DateTimeZone('UTC')) != $playlist->getArticlesModificationTime() && $playlist->getArticlesModificationTime() != null) {
throw new ResourcesConflictException('This list is already in a different state than the one in which it was loaded.', 409);
}
$playlist->setArticlesModificationTime(new \DateTime('now'));
$em->flush();
// The controller resolver needs a request to resolve the controller.
$stubRequest = new Request();
$actionsResults = array();
foreach ($actions as $actionKey => $action) {
foreach ($action as $method => $header) {
$urlMatcher->getContext()->setMethod($method);
switch ($method) {
case 'link':
$resource = $this->generateUrl('newscoop_gimme_articles_lists_linkarticle', array('id' => $playlist->getId()));
break;
case 'unlink':
$resource = $this->generateUrl('newscoop_gimme_articles_lists_unlinkarticle', array('id' => $playlist->getId()));
break;
}
$tempRequest = Request::create($resource);
try {
$route = $urlMatcher->match($tempRequest->getRequestUri());
} catch (\Exception $e) {
// If we don't have a matching route we return the original Link header
continue;
}
$stubRequest->attributes->replace($route);
$stubRequest->server = $request->server;
$stubRequest->headers->set('link', $header);
if (false === ($controller = $controllerResolver->getController($stubRequest))) {
continue;
}
$subEvent = new FilterControllerEvent($kernel, $controller, $stubRequest, HttpKernelInterface::SUB_REQUEST);
$kernelSubEvent = new GetResponseEvent($kernel, $stubRequest, HttpKernelInterface::SUB_REQUEST);
$dispatcher->dispatch(KernelEvents::REQUEST, $kernelSubEvent);
$dispatcher->dispatch(KernelEvents::CONTROLLER, $subEvent);
$controller = $subEvent->getController();
$arguments = $controllerResolver->getArguments($stubRequest, $controller);
try {
$result = call_user_func_array($controller, $arguments);
if (!is_object($result)) {
continue;
}
$actionsResults[$actionKey] = array('object' => $result, 'method' => $method, 'header' => $header);
} catch (\Exception $e) {
$actionsResults[$actionKey] = array('object' => $e->getMessage(), 'method' => $method, 'header' => $header);
continue;
}
}
}
return $actionsResults;
}