src/Repository/SeoRepository.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: adv
  5.  * Date: 04.12.15
  6.  * Time: 20:15
  7.  */
  8. namespace Slivki\Repository;
  9. use Doctrine\ORM\EntityRepository;
  10. use Slivki\Entity\Category;
  11. use Slivki\Entity\Seo;
  12. use Slivki\Util\CommonUtil;
  13. use Slivki\Util\SoftCache;
  14. /**
  15.  * @deprecated use \Slivki\Repository\Seo\SeoRepositoryInterface instead
  16.  * Move the required functions SeoRepositoryInterface
  17.  */
  18. final class SeoRepository extends EntityRepository
  19. {
  20.     const CACHE_EXPIRE_PERIOD 10 60;
  21.     const CACHE_NAME "seoAliases-20";
  22.     const CACHE_KEY_PREFIX 'entity-30-';
  23.     const CACHE_KEY_ALIAS 'alias-19-';
  24.     const RESOURCE_URL_OFFER_DETAILS 'Slivki:Default:details';
  25.     const RESOURCE_URL_SALE_DETAILS 'Slivki:Sale:details';
  26.     const RESOURCE_URL_OFFER_CATEGORY 'Slivki:Default:category';
  27.     const RESOURCE_URL_SALE_CATEGORY 'Slivki:Sale:category';
  28.     const RESOURCE_URL_PAGES 'Slivki:Page:details';
  29.     const RESOURCE_URL_CATEGORY_COMMENTS 'Slivki:Default:commentsByCategory';
  30.     const SALE_URL 'skidki-i-rasprodazhi';
  31.     const RESOURCE_URL_CITY 'Slivki:Default:index';
  32.     const RESOURCE_URL_MALL 'Slivki:Mall:details';
  33.     const RESOURCE_URL_SALE_INDEX 'Slivki:Sale:index';
  34.     const RESOURCE_URL_PARTNER_OFFERS_INDEX 'Slivki:PartnerOffer:index';
  35.     const RESOURCE_URL_DIRECTOR_DETAILS 'Slivki:Director:details';
  36.     const RESOURCE_URL_SALE_PRODUCT_DETAILS 'Slivki:SaleProduct:details';
  37.     const RESOURCE_URL_CATEGORY_RATING 'Slivki:Default:categoryRating';
  38.     const FLOWERS_CATEGORY_ID 867;
  39.     /**
  40.      * @return \Slivki\Entity\Seo
  41.      */
  42.     public function getByAlias($alias$domain 'www'$baseDomain '') {
  43.         $softCache = new SoftCache(self::CACHE_NAME);
  44.         $cacheKey self::CACHE_KEY_ALIAS $domain '-' $alias;
  45.         $seoCached $softCache->get($cacheKey);
  46.         if (!$seoCached) {
  47.             $seoCached $this->findBy(['domain' => $domain'mainAlias' => $alias'active' => true], ['ID' => 'desc'], 1);
  48.             if (!$seoCached && $domain != 'www' && $alias == '/') {
  49.                 $seoCached $this->findBy(['domain' => $domain'mainAlias' => '/' $domain], ['ID' => 'desc'], 1);
  50.             }
  51.             $seoCached count($seoCached) > $seoCached[0] : false;
  52.             if (!$seoCached) {
  53.                 $seoList $this->findBy(['mainAlias' => $alias], ['ID' => 'desc']);
  54.                 if ($seoList) {
  55.                     foreach ($seoList as $seo) {
  56.                         if ($seo->isActive()) {
  57.                             $this->getEntityManager()->detach($seo);
  58.                             $domain $seo->getDomain();
  59.                             $seo->setActive(false);
  60.                             $seo->setRedirectURL('https://' $domain $baseDomain $seo->getMainAlias());
  61.                             return $seo;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             if ($seoCached && !$seoCached->isActive()) {
  67.                 $seo $this->getSeoForEntity($seoCached->getResourceURL(), $seoCached->getEntityID());
  68.                 $seoCached->setRedirectURL($seo->getMainAlias());
  69.             }
  70.             $softCache->set($cacheKey$seoCachedself::CACHE_EXPIRE_PERIOD);
  71.         }
  72.         return $seoCached;
  73.     }
  74.     public function getByEntity($resourceURL$entityID) {
  75.         $softCache = new SoftCache(self::CACHE_NAME);
  76.         $cacheKey self::CACHE_KEY_PREFIX $resourceURL "-" $entityID;
  77.         $seoCached $softCache->get($cacheKey);
  78.         if (!$seoCached) {
  79.             $seoCached $this->getSeoForEntity($resourceURL$entityID);
  80.             $softCache->set($cacheKey$seoCachedself::CACHE_EXPIRE_PERIOD);
  81.         }
  82.         return $seoCached;
  83.     }
  84.     public function setAlias(Seo $seo) {
  85.         $mainAlias trim($seo->getMainAlias());
  86.         if ($mainAlias != "") {
  87.             if($mainAlias[0] != '/' && !CommonUtil::isBeginsWith($mainAlias'http')) {
  88.                 $mainAlias '/' $mainAlias;
  89.             }
  90.             $seo->setMainAlias($mainAlias);
  91.         }
  92.         /** @var Seo $seoExisting */
  93.         $seoExisting $this->findOneBy(['mainAlias' => $seo->getMainAlias(), 'active' => true'domain' => $seo->getDomain()]);
  94.         $entityManager $this->getEntityManager();
  95.         if ($seoExisting) {
  96.             if ($seoExisting->getEntityID() != $seo->getEntityID() || $seoExisting->getResourceURL() != $seo->getResourceURL()) {
  97.                 return false;
  98.             } else {
  99.                 if (!$seoExisting->isEqual($seo)) {
  100.                     $seoExisting->setTitle($seo->getTitle());
  101.                     $seoExisting->setMetaTitle($seo->getMetaTitle());
  102.                     $seoExisting->setMetaDescription($seo->getMetaDescription());
  103.                     $seoExisting->setMetaKeywords($seo->getMetaKeywords());
  104.                     $seoExisting->setPageDescription($seo->getPageDescription());
  105.                     $entityManager->flush($seoExisting);
  106.                     $this->reloadCacheForEntity($seo->getResourceURL(), $seo->getEntityID());
  107.                 }
  108.                 return true;
  109.             }
  110.         }
  111.         $currentSeo $this->findOneBy(['entityID' => $seo->getEntityID(), 'resourceURL' => $seo->getResourceURL(), 'active' => true]);
  112.         if ($currentSeo) {
  113.             $currentSeo->setActive(false);
  114.             $entityManager->flush($currentSeo);
  115.         }
  116.         $entityManager->persist($seo);
  117.         $entityManager->flush($seo);
  118.         $this->reloadCacheForSeo($seo$currentSeo);
  119.         return true;
  120.     }
  121.     public function reloadCacheForSeo(Seo $newSeoSeo $oldSeo null) {
  122.         $this->reloadCacheForEntity($newSeo->getResourceURL(), $newSeo->getEntityID(), $oldSeo);
  123.     }
  124.     public function reloadCacheForEntity($resourceURL$entityIDSeo $oldSeo null) {
  125.         $seo $this->getSeoForEntity($resourceURL$entityID);
  126.         $softCache = new SoftCache(self::CACHE_NAME);
  127.         $cacheKey self::CACHE_KEY_PREFIX $resourceURL "-" $entityID;
  128.         $softCache->set($cacheKey$seoself::CACHE_EXPIRE_PERIOD);
  129.         $cacheKey self::CACHE_KEY_ALIAS $seo->getMainAlias();
  130.         $softCache->set($cacheKey$seoself::CACHE_EXPIRE_PERIOD);
  131.         if ($oldSeo) {
  132.             $softCache->delete(self::CACHE_KEY_ALIAS $oldSeo->getMainAlias());
  133.         }
  134.     }
  135.     /**
  136.      * @deprecated
  137.      */
  138.     public function getSeoForEntity($resourceURL$entityID) {
  139.         $criteria = array(
  140.             "resourceURL" => $resourceURL,
  141.             "entityID" => $entityID,
  142.             "active" => true
  143.         );
  144.         $seo $this->findOneBy($criteria, ['ID' => 'desc']);
  145.         if (!$seo) {
  146.             $seo = new Seo();
  147.             $seo->setMainAlias("");
  148.         }
  149.         return $seo;
  150.     }
  151.     public function getOfferURL($offerID) {
  152.         return $this->getSeoForEntity(self::RESOURCE_URL_OFFER_DETAILS$offerID);
  153.     }
  154.     public function getCategoryCommentsSeo(Category $category) {
  155.         $seo $this->findOneBy(['resourceURL' => self::RESOURCE_URL_CATEGORY_COMMENTS'entityID' => $category->getID(), 'active' => true]);
  156.         if ($seo) {
  157.             return $seo;
  158.         }
  159.         $categorySeo $this->findOneBy(['resourceURL' => [self::RESOURCE_URL_OFFER_CATEGORYself::RESOURCE_URL_SALE_CATEGORY], 'entityID' => $category->getID(), 'active' => true]);
  160.         if (!$categorySeo) {
  161.             return false;
  162.         }
  163.         $categoryName trim($category->getName());
  164.         $seo = new Seo();
  165.         $seo->setActive(true);
  166.         $seo->setEntityID($category->getID());
  167.         $seo->setResourceURL(self::RESOURCE_URL_CATEGORY_COMMENTS);
  168.         $seo->setMainAlias('/otzyvy' $categorySeo->getMainAlias());
  169.         $seo->setTitle('Отзывы на ' $categoryName ' в Минске');
  170.         $seo->setMetaTitle($seo->getTitle());
  171.         $seo->setMetaKeywords('"' $categoryName '", отзывы');
  172.         $seo->setMetaDescription('Отзывы на ' $categoryName ' в Минске на Slivki.by. Только реальные отзывы пользователей об услугах и товарах!');
  173.         return $seo;
  174.     }
  175.     public function getCategoryURL(Category $category) {
  176.         switch ($category->getDomainObjectID()) {
  177.             case Category::OFFER_CATEGORY_ID:
  178.                 return $this->getByEntity(self::RESOURCE_URL_OFFER_CATEGORY$category->getID())->getMainAlias();
  179.             case Category::SALE_CATEGORY_ID:
  180.                 return $this->getByEntity(self::RESOURCE_URL_SALE_CATEGORY$category->getID())->getMainAlias();
  181.         }
  182.         return '';
  183.     }
  184.     public function getCityURL($cityID) {
  185.         $seo $this->getByEntity(self::RESOURCE_URL_CITY$cityID);
  186.         return $seo $seo->getMainAlias() : false;
  187.     }
  188. }