Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryRepository.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Catalog\Model;
9 
14 
19 {
23  protected $instances = [];
24 
28  protected $storeManager;
29 
33  protected $categoryFactory;
34 
38  protected $categoryResource;
39 
43  protected $metadataPool;
44 
48  private $extensibleDataObjectConverter;
49 
55  protected $useConfigFields = ['available_sort_by', 'default_sort_by', 'filter_price_range'];
56 
62  public function __construct(
63  \Magento\Catalog\Model\CategoryFactory $categoryFactory,
66  ) {
67  $this->categoryFactory = $categoryFactory;
68  $this->categoryResource = $categoryResource;
69  $this->storeManager = $storeManager;
70  }
71 
75  public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
76  {
77  $storeId = (int)$this->storeManager->getStore()->getId();
78  $existingData = $this->getExtensibleDataObjectConverter()
79  ->toNestedArray($category, [], \Magento\Catalog\Api\Data\CategoryInterface::class);
80  $existingData = array_diff_key($existingData, array_flip(['path', 'level', 'parent_id']));
81  $existingData['store_id'] = $storeId;
82 
83  if ($category->getId()) {
84  $metadata = $this->getMetadataPool()->getMetadata(
85  CategoryInterface::class
86  );
87 
88  $category = $this->get($category->getId(), $storeId);
89  $existingData[$metadata->getLinkField()] = $category->getData(
90  $metadata->getLinkField()
91  );
92 
93  if (isset($existingData['image']) && is_array($existingData['image'])) {
94  if (!empty($existingData['image']['delete'])) {
95  $existingData['image'] = null;
96  } else {
97  if (isset($existingData['image'][0]['name']) && isset($existingData['image'][0]['tmp_name'])) {
98  $existingData['image'] = $existingData['image'][0]['name'];
99  } else {
100  unset($existingData['image']);
101  }
102  }
103  }
104  } else {
105  $parentId = $category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId();
106  $parentCategory = $this->get($parentId, $storeId);
107  $existingData['path'] = $parentCategory->getPath();
108  $existingData['parent_id'] = $parentId;
109  }
110  $category->addData($existingData);
111  try {
112  $this->validateCategory($category);
113  $this->categoryResource->save($category);
114  } catch (\Exception $e) {
115  throw new CouldNotSaveException(
116  __(
117  'Could not save category: %1',
118  $e->getMessage()
119  ),
120  $e
121  );
122  }
123  unset($this->instances[$category->getId()]);
124  return $this->get($category->getId(), $storeId);
125  }
126 
130  public function get($categoryId, $storeId = null)
131  {
132  $cacheKey = $storeId ?? 'all';
133  if (!isset($this->instances[$categoryId][$cacheKey])) {
135  $category = $this->categoryFactory->create();
136  if (null !== $storeId) {
137  $category->setStoreId($storeId);
138  }
139  $category->load($categoryId);
140  if (!$category->getId()) {
141  throw NoSuchEntityException::singleField('id', $categoryId);
142  }
143  $this->instances[$categoryId][$cacheKey] = $category;
144  }
145  return $this->instances[$categoryId][$cacheKey];
146  }
147 
152  {
153  try {
154  $categoryId = $category->getId();
155  $this->categoryResource->delete($category);
156  } catch (\Exception $e) {
157  throw new StateException(
158  __(
159  'Cannot delete category with id %1',
160  $category->getId()
161  ),
162  $e
163  );
164  }
165  unset($this->instances[$categoryId]);
166  return true;
167  }
168 
172  public function deleteByIdentifier($categoryId)
173  {
174  $category = $this->get($categoryId);
175  return $this->delete($category);
176  }
177 
185  protected function validateCategory(Category $category)
186  {
187  $useConfigFields = [];
188  foreach ($this->useConfigFields as $field) {
189  if (!$category->getData($field)) {
190  $useConfigFields[] = $field;
191  }
192  }
193  $category->setData('use_post_data_config', $useConfigFields);
194  $validate = $category->validate();
195  if ($validate !== true) {
196  foreach ($validate as $code => $error) {
197  if ($error === true) {
198  $attribute = $this->categoryResource->getAttribute($code)->getFrontend()->getLabel();
199  throw new \Magento\Framework\Exception\LocalizedException(
200  __('The "%1" attribute is required. Enter and try again.', $attribute)
201  );
202  } else {
203  throw new \Magento\Framework\Exception\LocalizedException(__($error));
204  }
205  }
206  }
207  $category->unsetData('use_post_data_config');
208  }
209 
215  private function getExtensibleDataObjectConverter()
216  {
217  if ($this->extensibleDataObjectConverter === null) {
218  $this->extensibleDataObjectConverter = \Magento\Framework\App\ObjectManager::getInstance()
219  ->get(\Magento\Framework\Api\ExtensibleDataObjectConverter::class);
220  }
221  return $this->extensibleDataObjectConverter;
222  }
223 
227  private function getMetadataPool()
228  {
229  if (null === $this->metadataPool) {
231  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
232  }
233  return $this->metadataPool;
234  }
235 }
__()
Definition: __.php:13
__construct(\Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Magento\Store\Model\StoreManagerInterface $storeManager)
save(\Magento\Catalog\Api\Data\CategoryInterface $category)
$code
Definition: info.phtml:12