<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_AdvanceSeoManager
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace Webkul\AdvanceSeoManager\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class AfterCategorySave implements ObserverInterface
{
    /**
     *
     * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig
     * @param \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory
     * @param \Webkul\AdvanceSeoManager\Helper\Data $helper
     */
    public function __construct(
        \Magento\Config\Model\ResourceModel\Config $resourceConfig,
        \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory,
        \Webkul\AdvanceSeoManager\Helper\Data $helper,
        \Webkul\AdvanceSeoManager\Logger\PingLogger $logger
    ) {
           $this->resourceConfig = $resourceConfig;
           $this->collectionFactory = $collectionFactory;
           $this->helper = $helper;
           $this->logger = $logger;
    }

    /**
     * @param EventObserver $observer
     * @return $this|void
     */
    public function execute(EventObserver $observer)
    {
        $categoryObject = $observer->getEvent()->getCategory();
        if ($this->helper->getIsEnable()) {
            $collection = $this->collectionFactory->create();
            /* @var $collection \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection */
            foreach ($collection as $sitemap) {
                /* @var $sitemap \Magento\Sitemap\Model\Sitemap */
                try {
                    $sitemap->generateXml();
                } catch (\Exception $e) {
                    $this->logger->debug($e->getMessage());
                }
            }
        }
    }
}
