/**
  * List all unreferenced content types (from external sources)
  *
  * @param Request $request
  *        	@Route("/content-type/unreferenced", name="contenttype.unreferenced"))
  */
 public function unreferencedAction(Request $request)
 {
     /** @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     /** @var EnvironmentRepository $environmetRepository */
     $environmetRepository = $em->getRepository('AppBundle:Environment');
     $contentTypeRepository = $em->getRepository('AppBundle:ContentType');
     if ($request->isMethod('POST')) {
         if (null != $request->get('envId') && null != $request->get('name')) {
             $defaultEnvironment = $environmetRepository->find($request->get('envId'));
             if ($defaultEnvironment) {
                 $contentType = new ContentType();
                 $contentType->setName($request->get('name'));
                 $contentType->setPluralName($contentType->getName());
                 $contentType->setEnvironment($defaultEnvironment);
                 $contentType->setActive(true);
                 $contentType->setDirty(false);
                 $contentType->setOrderKey($contentTypeRepository->countContentType());
                 $em->persist($contentType);
                 $em->flush();
                 $this->addFlash('notice', 'The content type ' . $contentType->getName() . ' is now referenced');
                 return $this->redirectToRoute('contenttype.edit', ['id' => $contentType->getId()]);
             }
         }
         $this->addFlash('warning', 'Unreferenced content type not found.');
         return $this->redirectToRoute('contenttype.unreferenced');
     }
     /** @var ContentTypeRepository $contenttypeRepository */
     $contenttypeRepository = $em->getRepository('AppBundle:ContentType');
     $environments = $environmetRepository->findBy(['managed' => false]);
     /** @var  Client $client */
     $client = $this->get('app.elasticsearch');
     $referencedContentTypes = [];
     /** @var Environment $environment */
     foreach ($environments as $environment) {
         $alias = $environment->getAlias();
         $mapping = $client->indices()->getMapping(['index' => $alias]);
         foreach ($mapping as $indexName => $index) {
             foreach ($index['mappings'] as $name => $type) {
                 $already = $contenttypeRepository->findBy(['name' => $name]);
                 if (!$already || $already[0]->getDeleted()) {
                     $referencedContentTypes[] = ['name' => $name, 'alias' => $alias, 'envId' => $environment->getId()];
                 }
             }
         }
     }
     return $this->render('contenttype/unreferenced.html.twig', ['referencedContentTypes' => $referencedContentTypes]);
 }