Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GroupRepository.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
22  protected $groupResource;
23 
27  protected $groupFactory;
28 
32  protected $setRepository;
33 
37  protected $groupListFactory;
38 
43 
47  protected $joinProcessor;
48 
52  private $collectionProcessor;
53 
64  public function __construct(
66  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupListFactory,
67  \Magento\Eav\Model\Entity\Attribute\GroupFactory $groupFactory,
69  \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory $searchResultsFactory,
70  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
71  CollectionProcessorInterface $collectionProcessor = null
72  ) {
73  $this->groupResource = $groupResource;
74  $this->groupListFactory = $groupListFactory;
75  $this->groupFactory = $groupFactory;
76  $this->setRepository = $setRepository;
77  $this->searchResultsFactory = $searchResultsFactory;
78  $this->joinProcessor = $joinProcessor;
79  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
80  }
81 
85  public function save(\Magento\Eav\Api\Data\AttributeGroupInterface $group)
86  {
87  try {
88  $this->setRepository->get($group->getAttributeSetId());
89  } catch (NoSuchEntityException $ex) {
90  throw NoSuchEntityException::singleField('attributeSetId', $group->getAttributeSetId());
91  }
92 
93  if ($group->getAttributeGroupId()) {
95  $existingGroup = $this->groupFactory->create();
96  $this->groupResource->load($existingGroup, $group->getAttributeGroupId());
97 
98  if (!$existingGroup->getId()) {
99  throw NoSuchEntityException::singleField('attributeGroupId', $existingGroup->getId());
100  }
101  if ($existingGroup->getAttributeSetId() != $group->getAttributeSetId()) {
102  throw new StateException(
103  __("The attribute group doesn't belong to the provided attribute set.")
104  );
105  }
106  }
107 
108  try {
109  $this->groupResource->save($group);
110  } catch (\Exception $e) {
111  throw new StateException(__("The attributeGroup can't be saved."));
112  }
113  return $group;
114  }
115 
119  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
120  {
122  $collection = $this->groupListFactory->create();
123  $this->joinProcessor->process($collection);
124 
125  $this->collectionProcessor->process($searchCriteria, $collection);
126 
128  $searchResult = $this->searchResultsFactory->create();
129  $searchResult->setSearchCriteria($searchCriteria);
130  $searchResult->setItems($collection->getItems());
131  $searchResult->setTotalCount($collection->getSize());
132  return $searchResult;
133  }
134 
138  public function get($groupId)
139  {
141  $group = $this->groupFactory->create();
142  $this->groupResource->load($group, $groupId);
143  if (!$group->getId()) {
144  throw new NoSuchEntityException(
145  __('The group with the "%1" ID doesn\'t exist. Verify the ID and try again.', $groupId)
146  );
147  }
148  return $group;
149  }
150 
155  {
156  try {
157  $this->groupResource->delete($group);
158  } catch (\Exception $e) {
159  throw new StateException(
160  __(
161  'The attribute group with id "%1" can\'t be deleted.',
162  $group->getId()
163  ),
164  $e
165  );
166  }
167  return true;
168  }
169 
173  public function deleteById($groupId)
174  {
175  $this->delete(
176  $this->get($groupId)
177  );
178  return true;
179  }
180 
188  ) {
189  foreach ($searchCriteria->getFilterGroups() as $group) {
190  foreach ($group->getFilters() as $filter) {
191  if ($filter->getField() == 'attribute_set_id') {
192  return $filter->getValue();
193  }
194  }
195  }
196  return null;
197  }
198 
205  private function getCollectionProcessor()
206  {
207  if (!$this->collectionProcessor) {
208  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
209  'Magento\Eav\Model\Api\SearchCriteria\AttributeGroupCollectionProcessor'
210  );
211  }
212  return $this->collectionProcessor;
213  }
214 }
retrieveAttributeSetIdFromSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
$searchCriteria
save(\Magento\Eav\Api\Data\AttributeGroupInterface $group)
__construct(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group $groupResource, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupListFactory, \Magento\Eav\Model\Entity\Attribute\GroupFactory $groupFactory, \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository, \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory $searchResultsFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, CollectionProcessorInterface $collectionProcessor=null)