Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Code.php
Go to the documentation of this file.
1 <?php
9 
14 class Code
15 {
19  protected $_codeModel;
20 
24  protected $_entity;
25 
29  public function __construct(\Magento\GoogleOptimizer\Model\Code $code)
30  {
31  $this->_codeModel = $code;
32  }
33 
40  public function getCodeObjectByEntity(\Magento\Framework\Model\AbstractModel $entity)
41  {
42  $this->_entity = $entity;
43 
44  $this->_checkEntityIsEmpty();
45  if ($entity instanceof \Magento\Cms\Model\Page) {
46  $this->_codeModel->loadByEntityIdAndType($entity->getId(), $this->_getEntityType());
47  } else {
48  $this->_codeModel->loadByEntityIdAndType($entity->getId(), $this->_getEntityType(), $entity->getStoreId());
49  }
50 
51  return $this->_codeModel;
52  }
53 
60  protected function _getEntityType()
61  {
62  $type = $this->_getTypeString();
63 
64  if (empty($type)) {
65  throw new \InvalidArgumentException('The model class is not valid');
66  }
67 
68  return $type;
69  }
70 
76  protected function _getTypeString()
77  {
78  $type = '';
79  if ($this->_entity instanceof \Magento\Catalog\Model\Category) {
81  }
82 
83  if ($this->_entity instanceof \Magento\Catalog\Model\Product) {
85  }
86 
87  if ($this->_entity instanceof \Magento\Cms\Model\Page) {
89  }
90  return $type;
91  }
92 
99  protected function _checkEntityIsEmpty()
100  {
101  if (!$this->_entity->getId()) {
102  throw new \InvalidArgumentException('The model is empty');
103  }
104  return $this;
105  }
106 }
$type
Definition: item.phtml:13
$entity
Definition: element.phtml:22
getCodeObjectByEntity(\Magento\Framework\Model\AbstractModel $entity)
Definition: Code.php:40
__construct(\Magento\GoogleOptimizer\Model\Code $code)
Definition: Code.php:29
$code
Definition: info.phtml:12