/** * Don't allow creating variant group type if one already exists * * @param GroupTypeInterface $groupType * @param Constraint $constraint */ public function validate($groupType, Constraint $constraint) { if ($groupType->isVariant()) { $variantGroupType = $this->groupTypeRepository->getVariantGroupType(); if (null !== $variantGroupType && $variantGroupType->getId() !== $groupType->getId()) { $this->context->buildViolation($constraint->message)->addViolation(); } } }
/** * @param GroupInterface $group * @param array $data */ protected function setGroupType(GroupInterface $group, $data) { if (isset($data['type'])) { $typeCode = $data['type']; /** @var GroupType|null $type */ $type = $this->groupTypeRepository->findOneByIdentifier($typeCode); if (!$type) { throw new \LogicException(sprintf('Group Type with identifier "%s" not found', $typeCode)); } $group->setType($type); } }
/** * Create and configure a group instance * * @param string $groupTypeCode * * @return GroupInterface */ public function createGroup($groupTypeCode = null) { $group = $this->create(); if (null !== $groupTypeCode) { $groupType = $this->groupTypeRepository->findOneByIdentifier($groupTypeCode); if (null === $groupType) { throw new \InvalidArgumentException(sprintf('Group type with code "%s" was not found', $groupTypeCode)); } $group->setType($groupType); if ($group->getType()->isVariant()) { $group->setProductTemplate($this->productTemplateFactory->create()); } } return $group; }
/** * @param GroupInterface $variantGroup * @param string $type * * @throws \InvalidArgumentException */ protected function setType(GroupInterface $variantGroup, $type) { $groupType = $this->groupTypeRepository->findOneByIdentifier($type); if (null !== $groupType) { $variantGroup->setType($groupType); } else { throw new \InvalidArgumentException(sprintf('Type "%s" does not exist', $type)); } }
/** * Extract a variant group from column "groups" * * @param string $value * * @return array */ protected function extractVariantGroup($value) { $data = []; $groups = $this->fieldSplitter->splitCollection($value); foreach ($groups as $group) { $isVariant = $this->groupTypeRepository->getTypeByGroup($group); if ('1' === $isVariant) { $data['variant_group'][] = $group; } else { $data['groups'][] = $group; } } if (isset($data['variant_group']) && 1 < count($data['variant_group'])) { throw new \InvalidArgumentException(sprintf('The product cannot belong to many variant groups: %s', implode(', ', $data['variant_group']))); } elseif (isset($data['variant_group'])) { $data['variant_group'] = current($data['variant_group']); } return $data; }
/** * @param GroupInterface $group * @param string $type * * @throws \InvalidArgumentException */ protected function setType(GroupInterface $group, $type) { $groupType = $this->groupTypeRepository->findOneByIdentifier($type); if (null === $groupType) { throw new \InvalidArgumentException(sprintf('Type "%s" does not exist', $type)); } if ($groupType->isVariant()) { throw new \InvalidArgumentException(sprintf('Cannot process variant group "%s", only groups are accepted', $group->getCode())); } $group->setType($groupType); }
/** * List groups * * @Template * @AclAncestor("pim_enrich_group_index") * * @return Response */ public function indexAction() { $groupTypes = $this->groupTypeRepository->findTypeIds(false); return ['groupTypes' => $groupTypes]; }
/** * {@inheritdoc} * * @Template * @AclAncestor("pim_enrich_variant_group_index") * * @return Response */ public function indexAction() { return ['groupTypes' => $this->groupTypeRepository->findTypeIds(true)]; }