Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractTypeHandler.php
Go to the documentation of this file.
1 <?php
8 
15 
19 abstract class AbstractTypeHandler
20 {
21  const FIELD_IS_DELETE = 'is_delete';
22 
23  const FIELD_FILE = 'file';
24 
28  protected $deletedItems = [];
29 
33  protected $jsonHelper;
34 
38  protected $downloadableFile;
39 
43  protected $metadataPool;
44 
49  public function __construct(
52  ) {
53  $this->jsonHelper = $jsonHelper;
54  $this->downloadableFile = $downloadableFile;
55  }
56 
60  abstract public function getDataKey();
61 
65  abstract public function getIdentifierKey();
66 
72  public function save(Product $product, array $data)
73  {
74  $this->clear();
75  if (isset($data[$this->getDataKey()])) {
76  foreach ($data[$this->getDataKey()] as $item) {
77  if ($this->isDelete($item)) {
78  $this->addToDeleteQueue($item);
79  } else {
80  $this->saveItem($product, $item);
81  }
82  }
83  $this->processDelete();
84  }
85  return $this;
86  }
87 
91  abstract protected function createItem();
92 
99  abstract protected function setDataToModel(
101  array $data,
103  );
104 
110  abstract protected function linkToProduct(ComponentInterface $model, Product $product);
111 
116  protected function prepareItem(array $item)
117  {
118  unset($item[self::FIELD_IS_DELETE], $item[self::FIELD_FILE]);
119  if (isset($item[$this->getIdentifierKey()]) && !$item[$this->getIdentifierKey()]) {
120  unset($item[$this->getIdentifierKey()]);
121  }
122  return $item;
123  }
124 
128  abstract protected function processDelete();
129 
134  protected function isDelete(array $item)
135  {
136  return isset($item[self::FIELD_IS_DELETE]) && '1' == $item[self::FIELD_IS_DELETE];
137  }
138 
143  protected function getFiles(array $item)
144  {
145  $files = [];
146  if (isset($item[self::FIELD_FILE]) && $item[self::FIELD_FILE]) {
147  $files = $this->jsonHelper->jsonDecode($item[self::FIELD_FILE]);
148  }
149  return $files;
150  }
151 
157  abstract protected function setFiles(ComponentInterface $model, array $files);
158 
164  protected function saveItem(Product $product, array $item)
165  {
166  $files = $this->getFiles($item);
167  $item = $this->prepareItem($item);
168 
169  $model = $this->createItem();
170  $this->setDataToModel($model, $item, $product);
171  $this->setFiles($model, $files);
172  $model->save();
173  $this->linkToProduct($model, $product);
174  return $item;
175  }
176 
181  protected function addToDeleteQueue(array $item)
182  {
183  if (!empty($item[$this->getIdentifierKey()])) {
184  $this->deletedItems[] = $item[$this->getIdentifierKey()];
185  }
186  }
187 
193  protected function clear()
194  {
195  $this->deletedItems = [];
196  }
197 
202  protected function getMetadataPool()
203  {
204  if (!$this->metadataPool) {
205  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
206  }
207  return $this->metadataPool;
208  }
209 }
setDataToModel(ComponentInterface $model, array $data, Product $product)
linkToProduct(ComponentInterface $model, Product $product)
foreach($appDirs as $dir) $files