123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Model\Attribute;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Exception\StateException;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class GroupRepository implements \Magento\Eav\Api\AttributeGroupRepositoryInterface
- {
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group
- */
- protected $groupResource;
- /**
- * @var \Magento\Eav\Model\Entity\Attribute\GroupFactory
- */
- protected $groupFactory;
- /**
- * @var \Magento\Eav\Api\AttributeSetRepositoryInterface
- */
- protected $setRepository;
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
- */
- protected $groupListFactory;
- /**
- * @var \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory
- */
- protected $searchResultsFactory;
- /**
- * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
- */
- protected $joinProcessor;
- /**
- * @var CollectionProcessorInterface
- */
- private $collectionProcessor;
- /**
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group $groupResource
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupListFactory
- * @param \Magento\Eav\Model\Entity\Attribute\GroupFactory $groupFactory
- * @param \Magento\Eav\Api\AttributeSetRepositoryInterface $setRepository
- * @param \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterfaceFactory $searchResultsFactory
- * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
- * @param CollectionProcessorInterface $collectionProcessor
- * @codeCoverageIgnore
- */
- public function __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
- ) {
- $this->groupResource = $groupResource;
- $this->groupListFactory = $groupListFactory;
- $this->groupFactory = $groupFactory;
- $this->setRepository = $setRepository;
- $this->searchResultsFactory = $searchResultsFactory;
- $this->joinProcessor = $joinProcessor;
- $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
- }
- /**
- * {@inheritdoc}
- */
- public function save(\Magento\Eav\Api\Data\AttributeGroupInterface $group)
- {
- try {
- $this->setRepository->get($group->getAttributeSetId());
- } catch (NoSuchEntityException $ex) {
- throw NoSuchEntityException::singleField('attributeSetId', $group->getAttributeSetId());
- }
- if ($group->getAttributeGroupId()) {
- /** @var \Magento\Eav\Model\Entity\Attribute\Group $existingGroup */
- $existingGroup = $this->groupFactory->create();
- $this->groupResource->load($existingGroup, $group->getAttributeGroupId());
- if (!$existingGroup->getId()) {
- throw NoSuchEntityException::singleField('attributeGroupId', $existingGroup->getId());
- }
- if ($existingGroup->getAttributeSetId() != $group->getAttributeSetId()) {
- throw new StateException(
- __("The attribute group doesn't belong to the provided attribute set.")
- );
- }
- }
- try {
- $this->groupResource->save($group);
- } catch (\Exception $e) {
- throw new StateException(__("The attributeGroup can't be saved."));
- }
- return $group;
- }
- /**
- * {@inheritdoc}
- */
- public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
- {
- /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection $collection */
- $collection = $this->groupListFactory->create();
- $this->joinProcessor->process($collection);
- $this->collectionProcessor->process($searchCriteria, $collection);
- /** @var \Magento\Eav\Api\Data\AttributeGroupSearchResultsInterface $searchResult */
- $searchResult = $this->searchResultsFactory->create();
- $searchResult->setSearchCriteria($searchCriteria);
- $searchResult->setItems($collection->getItems());
- $searchResult->setTotalCount($collection->getSize());
- return $searchResult;
- }
- /**
- * {@inheritdoc}
- */
- public function get($groupId)
- {
- /** @var \Magento\Eav\Model\Entity\Attribute\Group $group */
- $group = $this->groupFactory->create();
- $this->groupResource->load($group, $groupId);
- if (!$group->getId()) {
- throw new NoSuchEntityException(
- __('The group with the "%1" ID doesn\'t exist. Verify the ID and try again.', $groupId)
- );
- }
- return $group;
- }
- /**
- * {@inheritdoc}
- */
- public function delete(\Magento\Eav\Api\Data\AttributeGroupInterface $group)
- {
- try {
- $this->groupResource->delete($group);
- } catch (\Exception $e) {
- throw new StateException(
- __(
- 'The attribute group with id "%1" can\'t be deleted.',
- $group->getId()
- ),
- $e
- );
- }
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function deleteById($groupId)
- {
- $this->delete(
- $this->get($groupId)
- );
- return true;
- }
- /**
- * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
- * @return null|string
- * @deprecated 101.0.3
- */
- protected function retrieveAttributeSetIdFromSearchCriteria(
- \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
- ) {
- foreach ($searchCriteria->getFilterGroups() as $group) {
- foreach ($group->getFilters() as $filter) {
- if ($filter->getField() == 'attribute_set_id') {
- return $filter->getValue();
- }
- }
- }
- return null;
- }
- /**
- * Retrieve collection processor
- *
- * @deprecated 101.0.0
- * @return CollectionProcessorInterface
- */
- private function getCollectionProcessor()
- {
- if (!$this->collectionProcessor) {
- $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
- 'Magento\Eav\Model\Api\SearchCriteria\AttributeGroupCollectionProcessor'
- );
- }
- return $this->collectionProcessor;
- }
- }
|