Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
GetCategoryWidgetByIdentifier.php 2.00 KiB
<?php
namespace NicolasBejean\CategoryWidget\Model;

use \Magento\Framework\Exception\LocalizedException;
use \NicolasBejean\CategoryWidget\Api\GetCategoryWidgetByIdentifierInterface;
use \NicolasBejean\CategoryWidget\Api\Data\CategoryWidgetInterface;
use \Magento\Framework\Exception\NoSuchEntityException;
use \NicolasBejean\CategoryWidget\Model\ResourceModel\CategoryWidget as CategoryWidgetResource;

/**
 * Class GetCategoryWidgetByIdentifier
 *
 * @category PHP
 * @package  NicolasBejean\CategoryWidget\Model
 * @author   Nicolas Béjean <nicolas@bejean.eu>
 * @license  https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence
 * @link     https://www.bejean.eu
 */
class GetCategoryWidgetByIdentifier implements GetCategoryWidgetByIdentifierInterface
{
    /**
     * @var CategoryWidgetFactory
     */
    private $categoryWidgetFactory;

    /**
     * @var CategoryWidgetResource
     */
    private $categoryWidgetResource;

    /**
     * @param CategoryWidgetFactory        $categoryWidgetFactory
     * @param CategoryWidgetResource       $categoryWidgetResource
     */
    public function __construct(
        CategoryWidgetFactory              $categoryWidgetFactory,
        CategoryWidgetResource             $categoryWidgetResource
    ) {
        $this->categoryWidgetFactory       = $categoryWidgetFactory;
        $this->categoryWidgetResource      = $categoryWidgetResource;
    }

    /**
     * @inheritdoc
     * @throws LocalizedException
     */
    public function execute(string $identifier, int $storeId) : CategoryWidgetInterface
    {
        $categoryWidget = $this->categoryWidgetFactory->create();
        $categoryWidget->setStoreId($storeId);
        $this->categoryWidgetResource->load($categoryWidget, $identifier, CategoryWidgetInterface::IDENTIFIER);

        if (!$categoryWidget->getId()) {
            throw new NoSuchEntityException(__('The category widget with the \'%1\' identifier doesn\'t exist.', $identifier));
        }

        return $categoryWidget;
    }
}