<commit_before><?php

namespace Oro\Bundle\EntityConfigBundle\ImportExport\Strategy;

use Oro\Bundle\EntityConfigBundle\Entity\FieldConfigModel;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\EntityExtendBundle\Model\EnumValue;
use Oro\Bundle\EntityExtendBundle\Provider\FieldTypeProvider;
use Oro\Bundle\EntityExtendBundle\Validator\FieldNameValidationHelper;
use Oro\Bundle\FormBundle\Validator\ConstraintFactory;
use Oro\Bundle\ImportExportBundle\Strategy\Import\AbstractImportStrategy;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * Import strategy for FieldConfigModel entity.
 */
class EntityFieldImportStrategy extends AbstractImportStrategy
{
    /** @var TranslatorInterface */
    protected $translator;

    /** @var ConstraintFactory */
    protected $constraintFactory;

    /** @var FieldTypeProvider */
    protected $fieldTypeProvider;

    /** @var FieldNameValidationHelper */
    protected $fieldValidationHelper;

    /**
     * @param TranslatorInterface $translator
     */
    public function setTranslator(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    /**
     * @param ConstraintFactory $constraintFactory
     */
    public function setConstraintFactory(ConstraintFactory $constraintFactory)
    {
        $this->constraintFactory = $constraintFactory;
    }

    /**
     * @param FieldTypeProvider $fieldTypeProvider
     */
    public function setFieldTypeProvider(FieldTypeProvider $fieldTypeProvider)
    {
        $this->fieldTypeProvider = $fieldTypeProvider;
    }

    /**
     * @param FieldNameValidationHelper $fieldValidationHelper
     */
    public function setFieldValidationHelper(FieldNameValidationHelper $fieldValidationHelper)
    {
        $this->fieldValidationHelper = $fieldValidationHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function process($entity)
    {
        $this->assertEnvironment($entity);

        /** @var FieldConfigModel $entity */
        $entity = $this->beforeProcessEntity($entity);
        if (!$entity) {
            return null;
        }
        $entity = $this->processEntity($entity);
        if (!$entity) {
            return null;
        }
        $entity = $this->afterProcessEntity($entity);
        if (!$entity) {
            return null;
        }
        return $this->validateAndUpdateContext($entity);
    }

    /**
     * @param FieldConfigModel $entity
     * @return null|FieldConfigModel
     */
    protected function processEntity(FieldConfigModel $entity)
    {
        $supportedTypes = $this->fieldTypeProvider->getSupportedFieldTypes();
        if (!in_array($entity->getType(), $supportedTypes, true)) {
            $this->addErrors($this->translator->trans('oro.entity_config.import.message.invalid_field_type'));

            return null;
        }
        return $entity;
    }

    /**
     * @param FieldConfigModel $entity
     * @return null|FieldConfigModel
     */
    protected function validateAndUpdateContext(FieldConfigModel $entity)
    {
        $validationGroups = $this->getValidationGroups();
        if ($this->isNewField($entity)) {
            $validationGroups = array_merge($validationGroups, $this->getValidationGroupsForNewField());
        }
        $errors = array_merge(
            (array)$this->strategyHelper->validateEntity(
                $entity,
                null,
                new GroupSequence($validationGroups)
            ),
            $this->validateEntityFields($entity)
        );

        if ($errors) {
            $this->addErrors($errors);
            return null;
        }

        $this->updateContextCounters($entity);
        $this->fieldValidationHelper->registerField($entity);

        return $entity;
    }

    /**
     * @param string|array $errors
     */
    protected function addErrors($errors)
    {
        $this->context->incrementErrorEntriesCount();
        $this->strategyHelper->addValidationErrors((array)$errors, $this->context);
    }

    /**
     * @param FieldConfigModel $entity
     */
    protected function updateContextCounters(FieldConfigModel $entity)
    {
        $fieldName = $entity->getFieldName();

        $existingField = $this->fieldValidationHelper->getSimilarExistingFieldData(
            $entity->getEntity()->getClassName(),<commit_msg>BB-17770: Attribute with same name can be imported in global organization (#25261)<commit_after><?php

namespace Oro\Bundle\EntityConfigBundle\ImportExport\Strategy;

use Oro\Bundle\EntityConfigBundle\Entity\FieldConfigModel;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\EntityExtendBundle\Model\EnumValue;
use Oro\Bundle\EntityExtendBundle\Provider\FieldTypeProvider;
use Oro\Bundle\EntityExtendBundle\Validator\FieldNameValidationHelper;
use Oro\Bundle\FormBundle\Validator\ConstraintFactory;
use Oro\Bundle\ImportExportBundle\Strategy\Import\AbstractImportStrategy;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * Import strategy for FieldConfigModel entity.
 */
class EntityFieldImportStrategy extends AbstractImportStrategy
{
    /** @var TranslatorInterface */
    protected $translator;

    /** @var ConstraintFactory */
    protected $constraintFactory;

    /** @var FieldTypeProvider */
    protected $fieldTypeProvider;

    /** @var FieldNameValidationHelper */
    protected $fieldValidationHelper;

    /**
     * @param TranslatorInterface $translator
     */
    public function setTranslator(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    /**
     * @param ConstraintFactory $constraintFactory
     */
    public function setConstraintFactory(ConstraintFactory $constraintFactory)
    {
        $this->constraintFactory = $constraintFactory;
    }

    /**
     * @param FieldTypeProvider $fieldTypeProvider
     */
    public function setFieldTypeProvider(FieldTypeProvider $fieldTypeProvider)
    {
        $this->fieldTypeProvider = $fieldTypeProvider;
    }

    /**
     * @param FieldNameValidationHelper $fieldValidationHelper
     */
    public function setFieldValidationHelper(FieldNameValidationHelper $fieldValidationHelper)
    {
        $this->fieldValidationHelper = $fieldValidationHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function process($entity)
    {
        $this->assertEnvironment($entity);

        /** @var FieldConfigModel $entity */
        $entity = $this->beforeProcessEntity($entity);
        if (!$entity) {
            return null;
        }
        $entity = $this->processEntity($entity);
        if (!$entity) {
            return null;
        }
        $entity = $this->afterProcessEntity($entity);
        if (!$entity) {
            return null;
        }
        return $this->validateAndUpdateContext($entity);
    }

    /**
     * @param FieldConfigModel $entity
     * @return null|FieldConfigModel
     */
    protected function processEntity(FieldConfigModel $entity)
    {
        $supportedTypes = $this->fieldTypeProvider->getSupportedFieldTypes();
        if (!in_array($entity->getType(), $supportedTypes, true)) {
            $this->addErrors($this->translator->trans('oro.entity_config.import.message.invalid_field_type'));

            return null;
        }
        return $entity;
    }

    /**
     * @param FieldConfigModel $entity
     * @return null|FieldConfigModel
     */
    protected function validateAndUpdateContext(FieldConfigModel $entity)
    {
        $errors = $this->getErrors($entity);

        if ($errors) {
            $this->addErrors($errors);
            return null;
        }

        $this->updateContextCounters($entity);
        $this->fieldValidationHelper->registerField($entity);

        return $entity;
    }

    /**
     * @param FieldConfigModel $entity
     * @return array
     */
    protected function getErrors(FieldConfigModel $entity): array
    {
        $validationGroups = $this->getValidationGroups();
        if ($this->isNewField($entity)) {
            $validationGroups = array_merge($validationGroups, $this->getValidationGroupsForNewField());
        }

        return array_merge(
            (array)$this->strategyHelper->validateEntity(
                $entity,
                null,
                new GroupSequence($validationGroups)
            ),
            $this->validateEntityFields($entity)
        );
    }

    /**
     * @param string|array $errors
     */
    protected function addErrors($errors)
    {
        $this->context->incrementErrorEntriesCount();
        $this->strategyHelper->addValidationErrors((array)$errors, $this->context);
    }

    /**
     * @param FieldConfigModel $entity
     */
    protected function updateContextCounters(FieldConfigModel $entity)
    {
        $fieldName = $entity->getFieldName();

        $existingField = $this->fieldValidationHelper->getSimilarExistingFieldData(
            $entity->getEntity()->getClassName(),
{'fraction_non_alphanumeric': 0.11224826675470452, 'fraction_numerical': 0.0011004732034774953, 'mean_word_length': 2.8623034424139395, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016228', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2318, 'n_words': 390}