vendor/wearemarketing/routingbundle/Enhancer/SeoContentRouteEnhancer.php line 62

Open in your IDE?
  1. <?php
  2. namespace WAM\Bundle\RoutingBundle\Enhancer;
  3. use Doctrine\Persistence\ObjectManager;
  4. use Sonata\SeoBundle\Seo\SeoPageInterface;
  5. use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
  6. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use WAM\Bundle\RoutingBundle\Entity\Interfaces\SeoMetaInterface;
  9. use WAM\Bundle\RoutingBundle\Entity\Interfaces\SeoMetaReadInterface;
  10. use WAM\Bundle\RoutingBundle\Entity\Interfaces\SeoMetaWriteInterface;
  11. use WAM\Bundle\RoutingBundle\Sitemap\Interfaces\AlternatesGeneratorInterface;
  12. /**
  13.  * This enhancer inject seo data from Router SeoMetaData field to Sonata SeoPage.
  14.  *
  15.  * @author Miguel Vilata <mvilata@wearemarketing.com>
  16.  */
  17. class SeoContentRouteEnhancer implements RouteEnhancerInterface
  18. {
  19.     protected SeoPageInterface $seoPage;
  20.     protected AlternatesGeneratorInterface $alternateRoutesGenerator;
  21.     protected ObjectManager $om;
  22.     /**
  23.      * @param SeoPageInterface $seoPage
  24.      * @param AlternatesGeneratorInterface $alternateRoutesGenerator
  25.      */
  26.     public function __construct(SeoPageInterface $seoPageAlternatesGeneratorInterface $alternateRoutesGeneratorObjectManager $om)
  27.     {
  28.         $this->seoPage $seoPage;
  29.         $this->alternateRoutesGenerator $alternateRoutesGenerator;
  30.         $this->om $om;
  31.     }
  32.     /**
  33.      * Return seo data present in route object and update it if neccesary
  34.      * from content object attached.
  35.      *
  36.      * @param array $defaults
  37.      * @param Request $request
  38.      *
  39.      * @return array
  40.      */
  41.     public function enhance(array $defaultsRequest $request)
  42.     {
  43.         $routeObject $defaults[RouteObjectInterface::ROUTE_OBJECT];
  44.         $contentObject $routeObject->getContent();
  45.         if ($contentObject instanceof SeoMetaReadInterface && $routeObject instanceof SeoMetaWriteInterface) {
  46.             $this->updateRouteSeoIfEmpty($contentObject$routeObject);
  47.         }
  48.         if ($routeObject instanceof SeoMetaReadInterface) {
  49.             $this->updateSeoPage($routeObject);
  50.         }
  51.         // Set alternate routes
  52.         foreach ($this->getAlternateRoutes($routeObject) as $alternateRoute) {
  53.             $this->seoPage->addLangAlternate($alternateRoute['url'], $alternateRoute['hreflang']);
  54.         }
  55.         return $defaults;
  56.     }
  57.     /**
  58.      * @param SeoMetaReadInterface $contentObject
  59.      * @param SeoMetaWriteInterface $routeObject
  60.      */
  61.     private function updateRouteSeoIfEmpty(SeoMetaReadInterface $contentObjectSeoMetaWriteInterface $routeObject)
  62.     {
  63.         $contentObjectSeoMetaData = [
  64.             SeoMetaInterface::SEO_META_TITLE => $contentObject->getSeoTitle(),
  65.             SeoMetaInterface::SEO_META_DESCRIPTION => $contentObject->getSeoDescription(),
  66.         ];
  67.         $routeSeoMetaData $routeObject->getSeoMetaData();
  68.         $isMetadataUpdated $contentObjectSeoMetaData == $routeSeoMetaData;
  69.         if ($isMetadataUpdated && ($routeObject->setSeoMetaDataIfEmpty($contentObjectSeoMetaData))) {
  70.             $this->om->persist(($routeObject));
  71.             $this->om->flush();
  72.         }
  73.     }
  74.     /**
  75.      * @param SeoMetaReadInterface $object
  76.      */
  77.     private function updateSeoPage(SeoMetaReadInterface $object)
  78.     {
  79.         $this->seoPage
  80.             ->setTitle($object->getSeoTitle())
  81.             ->addMeta('name''description'$object->getSeoDescription());
  82.         if ($object->hasSeoMetaRobots()) {
  83.             $this->seoPage->addMeta('name''robots'$object->getSeoMetaRobots());
  84.         }
  85.     }
  86.     /**
  87.      * @param $routeObject
  88.      *
  89.      * @return mixed
  90.      */
  91.     private function getAlternateRoutes($routeObject)
  92.     {
  93.         return $this->alternateRoutesGenerator->getAlternateRoutes($routeObject);
  94.     }
  95. }