Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
CategoryRepository Class Reference
Inheritance diagram for CategoryRepository:
CategoryRepositoryInterface

Public Member Functions

 __construct (\Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Magento\Store\Model\StoreManagerInterface $storeManager)
 
 save (\Magento\Catalog\Api\Data\CategoryInterface $category)
 
 delete (\Magento\Catalog\Api\Data\CategoryInterface $category)
 
 deleteByIdentifier ($categoryId)
 
- Public Member Functions inherited from CategoryRepositoryInterface
 save (\Magento\Catalog\Api\Data\CategoryInterface $category)
 
 get ($categoryId, $storeId=null)
 
 delete (\Magento\Catalog\Api\Data\CategoryInterface $category)
 

Protected Member Functions

 validateCategory (Category $category)
 

Protected Attributes

 $instances = []
 
 $storeManager
 
 $categoryFactory
 
 $categoryResource
 
 $metadataPool
 
 $useConfigFields = ['available_sort_by', 'default_sort_by', 'filter_price_range']
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 18 of file CategoryRepository.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Model\CategoryFactory  $categoryFactory,
\Magento\Catalog\Model\ResourceModel\Category  $categoryResource,
\Magento\Store\Model\StoreManagerInterface  $storeManager 
)
Parameters
\Magento\Catalog\Model\CategoryFactory$categoryFactory
\Magento\Catalog\Model\ResourceModel\Category$categoryResource
\Magento\Store\Model\StoreManagerInterface$storeManager

Definition at line 62 of file CategoryRepository.php.

Member Function Documentation

◆ delete()

{}

Definition at line 151 of file CategoryRepository.php.

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  }
__()
Definition: __.php:13

◆ deleteByIdentifier()

deleteByIdentifier (   $categoryId)

{Delete category by identifier

Parameters
int$categoryId
Returns
bool Will returned True if deleted
Exceptions
}

Implements CategoryRepositoryInterface.

Definition at line 172 of file CategoryRepository.php.

173  {
174  $category = $this->get($categoryId);
175  return $this->delete($category);
176  }

◆ save()

{}

Definition at line 75 of file CategoryRepository.php.

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  }
__()
Definition: __.php:13

◆ validateCategory()

validateCategory ( Category  $category)
protected

Validate category process

Parameters
Category$category
Returns
void
Exceptions

Definition at line 185 of file CategoryRepository.php.

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  }
__()
Definition: __.php:13
$code
Definition: info.phtml:12

Field Documentation

◆ $categoryFactory

$categoryFactory
protected

Definition at line 33 of file CategoryRepository.php.

◆ $categoryResource

$categoryResource
protected

Definition at line 38 of file CategoryRepository.php.

◆ $instances

$instances = []
protected

Definition at line 23 of file CategoryRepository.php.

◆ $metadataPool

$metadataPool
protected

Definition at line 43 of file CategoryRepository.php.

◆ $storeManager

$storeManager
protected

Definition at line 28 of file CategoryRepository.php.

◆ $useConfigFields

$useConfigFields = ['available_sort_by', 'default_sort_by', 'filter_price_range']
protected

Definition at line 55 of file CategoryRepository.php.


The documentation for this class was generated from the following file: