Skip to content

Commit

Permalink
magento#10045 Sitemap add item refactoring
Browse files Browse the repository at this point in the history
Add injectable item resolver to load sitemap items
  • Loading branch information
piotrkwiecinski committed Jul 19, 2017
1 parent ce5dbd5 commit 9867aca
Show file tree
Hide file tree
Showing 14 changed files with 1,145 additions and 75 deletions.
109 changes: 109 additions & 0 deletions app/code/Magento/Sitemap/Model/CategorySitemapItemResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sitemap\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory;
use Magento\Store\Model\ScopeInterface;

class CategorySitemapItemResolver implements SitemapItemResolverInterface
{
/**#@+
* Xpath config settings
*/
const XML_PATH_CATEGORY_CHANGEFREQ = 'sitemap/category/changefreq';
const XML_PATH_CATEGORY_PRIORITY = 'sitemap/category/priority';

/**#@-*/

/**
* Category factory
*
* @var CategoryFactory
*/
private $categoryFactory;

/**
* Sitemap item factory
*
* @var SitemapItemInterfaceFactory
*/
private $itemFactory;

/**
* Scope config
*
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* CategorySitemapItemResolver constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param CategoryFactory $categoryFactory
* @param SitemapItemInterfaceFactory $itemFactory
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
CategoryFactory $categoryFactory,
SitemapItemInterfaceFactory $itemFactory
) {
$this->categoryFactory = $categoryFactory;
$this->itemFactory = $itemFactory;
$this->scopeConfig = $scopeConfig;
}

/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$collection = $this->categoryFactory->create()->getCollection($storeId);
$items = array_map(function($item) use ($storeId) {
return $this->itemFactory->create([
'url' => $item->getUrl(),
'updatedAt' => $item->getUpdatedAt(),
'images' => $item->getImages(),
'priority' => $this->getCategoryPriority($storeId),
'changeFrequency' => $this->getCategoryChangeFrequency($storeId),
]);
}, $collection);

return $items;
}

/**
* Get page priority
*
* @param int $storeId
* @return string
*/
private function getCategoryPriority($storeId)
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_CATEGORY_PRIORITY,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Get page change frequency
*
* @param int $storeId
* @return string
*/
private function getCategoryChangeFrequency($storeId)
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_CATEGORY_CHANGEFREQ,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
}
113 changes: 113 additions & 0 deletions app/code/Magento/Sitemap/Model/CmsPageSitemapItemResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sitemap\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Sitemap\Model\ResourceModel\Cms\PageFactory;

class CmsPageSitemapItemResolver implements SitemapItemResolverInterface
{

/**#@+
* Xpath config settings
*/
const XML_PATH_PAGE_CHANGEFREQ = 'sitemap/page/changefreq';
const XML_PATH_PAGE_PRIORITY = 'sitemap/page/priority';

/**#@-*/

/**
* Cms page factory
*
* @var PageFactory
*/
private $cmsPageFactory;

/**
* Sitemap item factory
*
* @var SitemapItemInterfaceFactory
*/
private $itemFactory;

/**
* Scope config
*
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* CmsPageSitemapItemResolver constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param PageFactory $cmsPageFactory
* @param SitemapItemInterfaceFactory $itemFactory
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
PageFactory $cmsPageFactory,
SitemapItemInterfaceFactory $itemFactory
) {
$this->scopeConfig = $scopeConfig;
$this->cmsPageFactory = $cmsPageFactory;
$this->itemFactory = $itemFactory;
}

/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$collection = $this->cmsPageFactory->create()->getCollection($storeId);
var_dump($collection);
$items = array_map(function($item) use ($storeId) {
return $this->itemFactory->create([
'url' => $item->getUrl(),
'updatedAt' => $item->getUpdatedAt(),
'images' => $item->getImages(),
'priority' => $this->getPagePriority($storeId),
'changeFrequency' => $this->getPageChangeFrequency($storeId),
]);
}, $collection);

var_dump($items);

return $items;
}

/**
* Get page priority
*
* @param int $storeId
* @return string
*/
private function getPagePriority($storeId)
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_PAGE_PRIORITY,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Get page change frequency
*
* @param int $storeId
* @return string
*/
private function getPageChangeFrequency($storeId)
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_PAGE_CHANGEFREQ,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
}
44 changes: 44 additions & 0 deletions app/code/Magento/Sitemap/Model/CompositeSitemapItemResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sitemap\Model;


class CompositeSitemapItemResolver implements SitemapItemResolverInterface
{
/**
* Item resolvers
*
* @var SitemapItemResolverInterface[]
*/
private $itemResolvers;

/**
* CompositeSitemapItemResolver constructor.
*
* @param SitemapItemResolverInterface[] $itemResolvers
*/
public function __construct($itemResolvers = [])
{
$this->itemResolvers = $itemResolvers;
}

/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$items = [];

foreach ($this->itemResolvers as $resolver) {
foreach ($resolver->getItems($storeId) as $item) {
$items[] = $item;
}
}

return $items;
}
}
108 changes: 108 additions & 0 deletions app/code/Magento/Sitemap/Model/ProductSitemapItemResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sitemap\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory;
use Magento\Store\Model\ScopeInterface;

class ProductSitemapItemResolver implements SitemapItemResolverInterface
{
/**#@+
* Xpath config settings
*/
const XML_PATH_PRODUCT_CHANGEFREQ = 'sitemap/product/changefreq';
const XML_PATH_PRODUCT_PRIORITY = 'sitemap/product/priority';
/**#@-*/

/**
* Product factory
*
* @var ProductFactory
*/
private $productFactory;

/**
* Sitemap item factory
*
* @var SitemapItemInterfaceFactory
*/
private $itemFactory;

/**
* Scope config
*
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* ProductSitemapItemResolver constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param ProductFactory $productFactory
* @param SitemapItemInterfaceFactory $itemFactory
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
ProductFactory $productFactory,
SitemapItemInterfaceFactory $itemFactory
) {
$this->scopeConfig = $scopeConfig;
$this->productFactory = $productFactory;
$this->itemFactory = $itemFactory;
}

/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$collection = $this->productFactory->create()->getCollection($storeId);
$items = array_map(function($item) use ($storeId) {
return $this->itemFactory->create([
'url' => $item->getUrl(),
'updatedAt' => $item->getUpdatedAt(),
'images' => $item->getImages(),
'priority' => $this->getProductPriority($storeId),
'changeFrequency' => $this->getProductChangeFrequency($storeId),
]);
}, $collection);

return $items;
}

/**
* Get page priority
*
* @param int $storeId
* @return string
*/
private function getProductPriority($storeId)
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_PRODUCT_PRIORITY,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Get page change frequency
*
* @param int $storeId
* @return string
*/
private function getProductChangeFrequency($storeId)
{
return (string)$this->scopeConfig->getValue(
self::XML_PATH_PRODUCT_CHANGEFREQ,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
}
Loading

0 comments on commit 9867aca

Please sign in to comment.