/**
  * @param AttributeGroup $group
  *
  * @return array
  */
 public function getAllSources(AttributeGroup $group = null)
 {
     $sources = [];
     if ($this->isValid()) {
         $groups = $this->attributeGroupManager->getAllGroups();
         foreach ($groups as $group) {
             $sources[] = ['id' => $group->getCode(), 'name' => $group->getCode()];
         }
     }
     return $sources;
 }
 /**
  * Create a group in an attribute set.
  *
  * @param AbstractAttribute $pimAttribute
  *
  * @throws SoapCallException
  */
 protected function addGroupToAttributeSet(AbstractAttribute $pimAttribute)
 {
     $families = $pimAttribute->getFamilies();
     $group = $pimAttribute->getGroup();
     if (isset($group)) {
         $groupName = $group->getCode();
         foreach ($families as $family) {
             $familyMagentoId = $this->familyMappingManager->getIdFromFamily($family, $this->getSoapUrl());
             if (null === $familyMagentoId) {
                 $magentoAttributeSets = $this->webservice->getAttributeSetList();
                 if (array_key_exists($family->getCode(), $magentoAttributeSets)) {
                     $familyMagentoId = $magentoAttributeSets[$family->getCode()];
                 }
             }
             try {
                 $magentoGroupId = $this->webservice->addAttributeGroupToAttributeSet($familyMagentoId, $groupName);
                 $this->attributeGroupMappingManager->registerGroupMapping($group, $family, $magentoGroupId, $this->getSoapUrl());
             } catch (SoapCallException $e) {
                 if (static::SOAP_FAULT_GROUP_ALREADY_IN_SET === $e->getPrevious()->faultcode) {
                     echo "DEBUG: Group " . $groupName . " already exists in attribute set " . $familyMagentoId . "\n";
                 } else {
                     throw $e;
                 }
             }
         }
     }
 }
 /**
  * Create a group in an attribute set
  *
  * @param AbstractAttribute $pimAttribute
  *
  * @throws \Exception
  * @throws \SoapCallException
  *
  * @return void
  */
 protected function addGroupToAttributeSet($pimAttribute)
 {
     $families = $pimAttribute->getFamilies();
     $group = $pimAttribute->getGroup();
     if (isset($group)) {
         $groupName = $group->getCode();
         foreach ($families as $family) {
             $familyMagentoId = $this->familyMappingManager->getIdFromFamily($family, $this->getSoapUrl());
             if (null === $familyMagentoId) {
                 $magentoAttributeSets = $this->webservice->getAttributeSetList();
                 if (array_key_exists($family->getCode(), $magentoAttributeSets)) {
                     $familyMagentoId = $magentoAttributeSets[$family->getCode()];
                 }
             }
             try {
                 $magentoGroupId = $this->webservice->addAttributeGroupToAttributeSet($familyMagentoId, $groupName);
                 $this->attributeGroupMappingManager->registerGroupMapping($group, $family, $magentoGroupId, $this->getSoapUrl());
             } catch (SoapCallException $e) {
                 if (!strpos($e->getMessage(), 'already') !== false) {
                     throw $e;
                 }
             }
         }
     }
 }
 function it_sends_attribute_with_group_and_family_to_create_on_magento_webservice($webservice, AbstractAttribute $attribute, AttributeMappingManager $attributeMappingManager, AttributeGroupMappingManager $attributeGroupMappingManager, FamilyMappingManager $familyMappingManager, AttributeGroup $group, Family $family, $magentoMappingMerger, MappingCollection $mapping)
 {
     $attributes = [[$attribute, ['create' => ['attributeName' => 'attribute_code']]]];
     $this->setMagentoUrl(null);
     $this->setWsdlUrl('/api/soap/?wsdl');
     $attribute->getCode()->willReturn('attributeName');
     $attribute->getFamilies()->willReturn([$family]);
     $attribute->getGroup()->willReturn($group);
     $magentoMappingMerger->getMapping()->willReturn($mapping);
     $mapping->getTarget($attribute)->willReturn(12);
     $mapping->getSource(12)->willReturn(12);
     $group->getCode()->willReturn('group_name');
     $familyMappingManager->getIdFromFamily(Argument::any(), '/api/soap/?wsdl')->willReturn(414);
     $webservice->addAttributeGroupToAttributeSet(414, 'group_name')->shouldBeCalled()->willReturn(797);
     $webservice->createAttribute(Argument::any())->willReturn(12);
     $webservice->addAttributeToAttributeSet(12, 414, 797)->shouldBeCalled();
     $attributeGroupMappingManager->registerGroupMapping($group, $family, 797, '/api/soap/?wsdl')->shouldBeCalled();
     $attributeGroupMappingManager->getIdFromGroup($group, $family, '/api/soap/?wsdl')->willReturn(797);
     $attributeMappingManager->registerAttributeMapping($attribute, 12, '/api/soap/?wsdl')->shouldBeCalled();
     $this->write($attributes);
 }