|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * @author tjitse (Vendic) |
| 6 | + * Created on 22/03/2019 15:31 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Vendic\VueStorefrontSitemap\Cron; |
| 10 | + |
| 11 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 12 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 13 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 14 | +use SitemapPHP\Sitemap; |
| 15 | +use Vendic\VueStorefrontSitemap\Model\CategoryCollection; |
| 16 | +use Vendic\VueStorefrontSitemap\Model\Configuration; |
| 17 | +use Vendic\VueStorefrontSitemap\Model\ProductCollection; |
| 18 | +use Vendic\VueStorefrontSitemap\Model\SitemapFactory; |
| 19 | + |
| 20 | +class GenerateSitemap |
| 21 | +{ |
| 22 | + const SITEMAP_NAME = 'sitemap.xml'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var DirectoryList |
| 26 | + */ |
| 27 | + protected $directoryList; |
| 28 | + /** |
| 29 | + * @var SitemapFactory |
| 30 | + */ |
| 31 | + protected $sitemapFactory; |
| 32 | + /** |
| 33 | + * @var ProductCollection |
| 34 | + */ |
| 35 | + protected $productCollection; |
| 36 | + /** |
| 37 | + * @var Sitemap |
| 38 | + */ |
| 39 | + protected $sitemap; |
| 40 | + /** |
| 41 | + * @var Configuration |
| 42 | + */ |
| 43 | + protected $configuration; |
| 44 | + /** |
| 45 | + * @var CategoryCollection |
| 46 | + */ |
| 47 | + protected $categoryCollection; |
| 48 | + |
| 49 | + public function __construct( |
| 50 | + CategoryCollection $categoryCollection, |
| 51 | + Configuration $configuration, |
| 52 | + ProductCollection $productCollection, |
| 53 | + DirectoryList $directoryList, |
| 54 | + SitemapFactory $sitemapFactory |
| 55 | + ) { |
| 56 | + $this->directoryList = $directoryList; |
| 57 | + $this->sitemapFactory = $sitemapFactory; |
| 58 | + $this->productCollection = $productCollection; |
| 59 | + $this->configuration = $configuration; |
| 60 | + $this->categoryCollection = $categoryCollection; |
| 61 | + } |
| 62 | + |
| 63 | + public function execute() : void |
| 64 | + { |
| 65 | + // Collect settings |
| 66 | + $domain = $this->configuration->getVueStorefrontUrl(); |
| 67 | + $path = $this->getPubPath(); |
| 68 | + |
| 69 | + // Sitemap configuration |
| 70 | + $this->sitemap = $this->sitemapFactory->create($domain); |
| 71 | + $this->sitemap->setPath($path); |
| 72 | + $this->sitemap->setFilename('sitemap'); |
| 73 | + |
| 74 | + // Add data |
| 75 | + $this->addHomepageToSitemap(); |
| 76 | + $this->addCategoriesToSitemap(); |
| 77 | + $this->addProductsToSitemap(); |
| 78 | + |
| 79 | + // Generate |
| 80 | + $this->sitemap->createSitemapIndex($domain, 'Today'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 85 | + */ |
| 86 | + private function getPubPath() : string |
| 87 | + { |
| 88 | + return $this->directoryList->getPath('pub') . '/'; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return \Magento\Catalog\Model\ResourceModel\Product\Collection |
| 93 | + */ |
| 94 | + private function getActiveProducts() |
| 95 | + { |
| 96 | + return $this->productCollection->get(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return \Magento\Catalog\Model\ResourceModel\Category\Collection |
| 101 | + */ |
| 102 | + private function getActiveCategories() |
| 103 | + { |
| 104 | + return $this->categoryCollection->get(); |
| 105 | + } |
| 106 | + |
| 107 | + protected function addProductsToSitemap(): void |
| 108 | + { |
| 109 | + $activeProducts = $this->getActiveProducts(); |
| 110 | + if ($activeProducts->count() >= 1) { |
| 111 | + /** @var ProductInterface $product */ |
| 112 | + foreach ($activeProducts->getItems() as $product) { |
| 113 | + $productUrl = '/' . $product->getSku() . '/' . $product->getUrlKey(); |
| 114 | + $this->sitemap->addItem( |
| 115 | + $productUrl, |
| 116 | + 1.0, |
| 117 | + 'daily', |
| 118 | + $product->getUpdatedAt() |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + protected function addCategoriesToSitemap(): void |
| 125 | + { |
| 126 | + $activeCategories = $this->getActiveCategories(); |
| 127 | + if ($activeCategories->count() >= 1) { |
| 128 | + /** @var CategoryInterface $category */ |
| 129 | + foreach ($activeCategories->getItems() as $category) { |
| 130 | + $categoryUrl = '/' . $category->getUrlKey() . '-' . $category->getId(); |
| 131 | + $this->sitemap->addItem( |
| 132 | + $categoryUrl, |
| 133 | + 1.0, |
| 134 | + 'daily', |
| 135 | + $category->getUpdatedAt() |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + protected function addHomepageToSitemap() : void |
| 142 | + { |
| 143 | + $this->sitemap->addItem( |
| 144 | + '/' |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments