/**
  * Remove an attribute from product template
  *
  * @param AttributeInterface $attribute
  */
 protected function removeFromProductTemplate(AttributeInterface $attribute)
 {
     if (null === $this->productTplBuilder || null === $this->productTplRepository) {
         return;
     }
     foreach ($this->productTplRepository->findAll() as $productTemplate) {
         if ($productTemplate->hasValueForAttribute($attribute)) {
             $this->productTplBuilder->removeAttribute($productTemplate, $attribute);
             $attributeCodes = $productTemplate->getAttributeCodes();
             empty($attributeCodes) ? $this->objectManager->remove($productTemplate) : $this->objectManager->persist($productTemplate);
         }
     }
 }
 function it_updates_a_product_template_if_attribute_has_been_deleted($eventDispatcher, $objectManager, $optionsResolver, AttributeInterface $attribute, ProductTemplateBuilderInterface $productTemplateBuilder, ProductTemplateRepositoryInterface $productTemplateRepository, ProductTemplate $productTemplate)
 {
     $optionsResolver->resolveRemoveOptions([])->willReturn(['flush' => true]);
     $objectManager->remove($attribute)->shouldBeCalled();
     $objectManager->flush()->shouldBeCalled();
     $attribute->getCode()->willReturn('test');
     $productTemplate->getValuesData()->willReturn(['test', 'test2']);
     $productTemplateRepository->findAll()->willReturn([$productTemplate]);
     $productTemplate->hasValueForAttribute($attribute)->willReturn(true);
     $productTemplateBuilder->removeAttribute($productTemplate, $attribute)->shouldBeCalled();
     $productTemplate->getAttributeCodes()->willReturn(['test2']);
     $objectManager->persist($productTemplate)->shouldBeCalled();
     $objectManager->flush()->shouldBeCalled();
     $this->remove($attribute);
 }
 /**
  * @param RemoveEvent $event
  */
 public function preRemove(RemoveEvent $event)
 {
     $subject = $event->getSubject();
     if (!$subject instanceof AttributeInterface) {
         return;
     }
     $productTemplates = $this->productTplRepository->findByAttribute($subject);
     foreach ($productTemplates as $productTemplate) {
         $this->productTplBuilder->removeAttribute($productTemplate, $subject);
         $attributeCodes = $productTemplate->getAttributeCodes();
         if (empty($attributeCodes)) {
             $this->objectManager->remove($productTemplate);
         } else {
             $this->objectManager->persist($productTemplate);
         }
     }
 }
 /**
  * Remove an attribute form a variant group
  *
  * @param Request $request
  * @param int     $groupId
  * @param int     $attributeId
  *
  * @AclAncestor("pim_enrich_group_remove_attribute")
  *
  * @throws NotFoundHttpException
  *
  * @return RedirectResponse
  */
 public function removeAttributeAction(Request $request, $groupId, $attributeId)
 {
     $group = $this->findVariantGroupOr404($groupId);
     $attribute = $this->findAttributeOr404($attributeId);
     $template = $group->getProductTemplate();
     if (null !== $template) {
         $this->templateBuilder->removeAttribute($template, $attribute);
         $this->groupSaver->save($group);
     }
     if ($request->isXmlHttpRequest()) {
         return new Response('', 204);
     }
     return $this->redirectToRoute('pim_enrich_variant_group_edit', ['id' => $groupId]);
 }