EntityCmsPage.php 1.87 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\GoogleOptimizer\Block\Adminhtml\Cms\Page;

use Magento\Framework\DataObject;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\GoogleOptimizer\Model\Code as GoogleOptimizerCode;

class EntityCmsPage extends DataObject
{
    /**
     * @var \Magento\Framework\Registry
     */
    private $coreRegistry;

    /**
     * @var GoogleOptimizerCode
     */
    private $codeModel;

    /**
     * Google Optimizer binded entity
     *
     * @var \Magento\Cms\Model\Page
     */
    private $entity;

    /**
     * @param \Magento\Framework\Registry $registry
     * @param GoogleOptimizerCode $code
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Registry $registry,
        GoogleOptimizerCode $code,
        array $data = []
    ) {
        $this->coreRegistry = $registry;
        $this->codeModel = $code;
        parent::__construct($data);
    }

    /**
     * Returns Code Model
     *
     * @return GoogleOptimizerCode|null
     * @throws NoSuchEntityException
     */
    public function getCode()
    {
        $code = null;
        $entity = $this->getEntity();
        if ($entity->getId()) {
            $this->codeModel->loadByEntityIdAndType($entity->getId(), GoogleOptimizerCode::ENTITY_TYPE_PAGE);
            $code = $this->codeModel;
        }
        return $code;
    }

    /**
     * Retrieves Google Optimizer binded entity
     *
     * @return \Magento\Cms\Model\Page|mixed
     * @throws NoSuchEntityException
     */
    private function getEntity()
    {
        if (!$this->entity) {
            $this->entity = $this->coreRegistry->registry('cms_page');
            if (!$this->entity) {
                throw new NoSuchEntityException();
            }
        }
        return $this->entity;
    }
}