lib/boab/cms-bundle/src/Manager/ContentTypeManager.php line 103

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\Manager;
  3. use Symfony\Component\Form\FormFactoryInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Boab\CmsBundle\Repository\ContentRepositoryInterface;
  6. use Boab\CmsBundle\Entity\RouteContentInterface;
  7. use Boab\CmsBundle\Entity\ContentInterface;
  8. use Boab\CmsBundle\Event\ContentPostPersistEvent;
  9. use Boab\CmsBundle\Event\ContentPrePersistEvent;
  10. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  11. use Doctrine\ORM\EntityManager;
  12. use InvalidArgumentException;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Boab\CmsBundle\Events;
  15. use Boab\CmsBundle\Util\DataTable\DataTableContextInterface;
  16. use Exception;
  17. use Symfony\Component\EventDispatcher\EventDispatcher;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. class ContentTypeManager implements ContentTypeManagerInterface
  20. {
  21.     private $contentRepository;
  22.     private $formFactory;
  23.     private $entityManager;
  24.     private $router;
  25.     private $eventDispatcher;
  26.     private $contentTypes = [];
  27.     public function __construct(
  28.         ContentRepositoryInterface $contentRepository
  29.         EntityManager $entityManager
  30.         FormFactoryInterface $formFactory,
  31.         RouterInterface $router,
  32.         EventDispatcherInterface $eventDispatcher)
  33.     {
  34.         $this->contentRepository $contentRepository;
  35.         $this->entityManager $entityManager;
  36.         $this->formFactory $formFactory;
  37.         $this->router $router;
  38.         $this->eventDispatcher $eventDispatcher;
  39.     }
  40.     public function addContentType(TypeManagerInterface $contentType)
  41.     {
  42.         $this->contentTypes[] = $contentType;
  43.     }    
  44.     public function getContentTypes() : array
  45.     {
  46.         return $this->contentTypes;
  47.     }
  48.     public function getType(string $type):TypeManagerInterface
  49.     {
  50.         foreach ($this->getContentTypes() as $typeManager) {
  51.             if($typeManager->supports($type)){
  52.                 return $typeManager;
  53.             }
  54.         }
  55.         throw new \InvalidArgumentException(sprintf("Invalid content type identifier ( %s )"$type));
  56.     }
  57.     public function getTypeByClass($class)
  58.     {
  59.         foreach ($this->getContentTypes() as $type) {
  60.             if ($class === $type->getEntityClass()) {
  61.                 return $type;
  62.             }
  63.         }
  64.         throw new \InvalidArgumentException(sprintf("Invalid content type class ( %s )"$class));
  65.     }
  66.     public function getTypeByObject(ContentInterface $object)
  67.     {
  68.         return $this->getTypeByClass(get_class($object));
  69.     }
  70.     public function getTypeByContent(ContentInterface $object)
  71.     {
  72.         return $this->getTypeByClass(get_class($object));
  73.     }    
  74.     public function getCollection(Request $requestint $pageNumber)
  75.     {
  76.         $typeId $request->get('routeDocument')->getContentTypeId();
  77.         $typeManager $this->getType($typeId);
  78.         if(!$typeManager){
  79.             throw new InvalidArgumentException(sprintf("The type manager does not exists for typeId %s"$typeId));
  80.         }
  81.         
  82.         return $typeManager->getCollection($request$pageNumber);
  83.     }
  84.     public function getContent(Request $request)
  85.     {
  86.         $route $request->get('routeDocument');
  87.         if(!$route instanceof RouteContentInterface){
  88.             throw new \Exception(sprintf('Your route must implements %s to call the method %s'RouteContentInterface::class, __METHOD__));
  89.         }
  90.         $typeManager $this->getType($route->getContentType());
  91.         return $typeManager->getContent($request);
  92.     } 
  93.     public function getContents(array $params)
  94.     {
  95.         return $this->contentRepository->findContents($params);
  96.     }
  97.     
  98.     public function searchContent(array $params)
  99.     {
  100.         return $this->contentRepository->findContentBySearchTerm($params);
  101.     }
  102.     public function create(ContentInterface $content): ContentInterface
  103.     {
  104.         $typeManager $this->getTypeByObject($content);
  105.         if(method_exists($typeManager,'prePersist')){
  106.             $typeManager->prePersist($content);
  107.         }
  108.         $content->setDateCreated(new \DateTime('now'));
  109.             
  110.         $event = new ContentPrePersistEvent($content);
  111.         $this->eventDispatcher->dispatch($eventEvents::CONTENT_PRE_PERSIST);
  112.         try{
  113.             $this->entityManager->persist($content);
  114.             $this->entityManager->flush();
  115.         }catch(UniqueConstraintViolationException $e){
  116.             throw new Exception("An content with the same title exists already");
  117.         }
  118.         
  119.         $postEvent = new ContentPostPersistEvent($content);  
  120.         $this->eventDispatcher->dispatch($postEventEvents::CONTENT_POST_PERSIST);
  121.         return $content;
  122.     }
  123.     public function update(ContentInterface $content)
  124.     {
  125.         $typeManager $this->getTypeByObject($content);
  126.         if(method_exists($typeManager,'preUpdate')){
  127.             $typeManager->preUpdate($content);
  128.         }
  129.         $this->entityManager->persist($content);
  130.         $this->entityManager->flush();  
  131.         $this->entityManager->refresh($content);      
  132.     }
  133.     public function remove(ContentInterface $content)
  134.     {
  135.         $typeManager $this->getTypeByObject($content);
  136.         if(method_exists($typeManager,'preRemove')){
  137.             $typeManager->preRemove($content);
  138.         }        
  139.         $this->contentRepository->delete($content);
  140.     }
  141.     public function getSearchForm($type, array $options=[])
  142.     {
  143.         $typeManager $this->getType($type);
  144.         $builder $this->formFactory->createNamedBuilder($type);
  145.         $builder->setMethod('GET');
  146.         if(!method_exists($typeManager'buildSearchForm')){
  147.             return;
  148.         }
  149.         $typeManager->buildSearchForm($builder, [
  150.             "type"=>$type
  151.         ]);
  152.         $action $this->router->generate("app.search_content", ["type"=>$type]);
  153.         $builder->setAction($action);
  154.         return $builder->getForm();
  155.     }
  156.     public function filterResults($data=[], int $page):?iterable
  157.     {
  158.         $typeManager $this->getType($data["type"]);
  159.         $collection $typeManager->filterResults($data["query"], $page);
  160.         
  161.         return $collection;
  162.     }
  163. }