<commit_before><?php

declare(strict_types=1);

namespace AlterPHP\EasyAdminExtensionBundle\Helper;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
 * This file is part of the EasyAdmin Extension package.
 */
class EmbeddedListHelper
{
    /**
     * @var ManagerRegistry
     */
    private $doctrine;

    /**
     * @var array
     */
    private $easyAdminConfig;

    /**
     * @param ManagerRegistry $doctrine
     * @param array           $easyAdminConfig
     */
    public function __construct(ManagerRegistry $doctrine, array $easyAdminConfig)
    {
        $this->doctrine = $doctrine;
        $this->easyAdminConfig = $easyAdminConfig;
    }

    /**
     * Returns EasyAdmin entity entry name for a parent FQCN and property for embedded list.
     *
     * @param string $parentFqcn
     * @param string $parentProperty
     *
     * @return mixed
     *
     * @throws \RuntimeException
     */
    public function getEntityFqcnFromParent(string $parentFqcn, string $parentProperty)
    {
        $parentClassMetadata = $this->doctrine->getManagerForClass($parentFqcn)->getClassMetadata($parentFqcn);

        // Required to use getAssociationMappings method
        if (!$parentClassMetadata instanceof ClassMetadataInfo) {
            return;
        }

        try {
            $entityFqcn = $parentClassMetadata->getAssociationTargetClass($parentProperty);
        } catch (\Exception $e) {
            return;
        }

        return $entityFqcn;
    }

    /**
     * Returns EasyAdmin entity entry name for a FQCN.
     *
     * @param string $entityFqcn
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    public function guessEntityEntry(string $entityFqcn)
    {
        $matchingEntityConfigs = \array_filter(
            $this->easyAdminConfig['entities'],
            function ($entityConfig) use ($entityFqcn) {
                return $entityFqcn === $entityConfig['class'];
            }
        );

        if (empty($matchingEntityConfigs)) {
            throw new \RuntimeException(
                \sprintf('No entity defined in EasyAdmin configuration matches %s FQCN.', $entityFqcn)
            );
        }

        if (1 < \count($matchingEntityConfigs)) {
            throw new \RuntimeException(
                \sprintf(
                    'More than 1 entity defined in EasyAdmin configuration matches %s FQCN.'.
                    ' Try setting option "entity" (in type_options for NEW/EDIT/FORM view, or template_options for SHOW view)'.
                    ' with one of [%s].',
                    $entityFqcn,
                    \implode(', ', $matchingEntityConfigs)
                )<commit_msg>Fix error message<commit_after><?php

declare(strict_types=1);

namespace AlterPHP\EasyAdminExtensionBundle\Helper;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
 * This file is part of the EasyAdmin Extension package.
 */
class EmbeddedListHelper
{
    /**
     * @var ManagerRegistry
     */
    private $doctrine;

    /**
     * @var array
     */
    private $easyAdminConfig;

    /**
     * @param ManagerRegistry $doctrine
     * @param array           $easyAdminConfig
     */
    public function __construct(ManagerRegistry $doctrine, array $easyAdminConfig)
    {
        $this->doctrine = $doctrine;
        $this->easyAdminConfig = $easyAdminConfig;
    }

    /**
     * Returns EasyAdmin entity entry name for a parent FQCN and property for embedded list.
     *
     * @param string $parentFqcn
     * @param string $parentProperty
     *
     * @return mixed
     *
     * @throws \RuntimeException
     */
    public function getEntityFqcnFromParent(string $parentFqcn, string $parentProperty)
    {
        $parentClassMetadata = $this->doctrine->getManagerForClass($parentFqcn)->getClassMetadata($parentFqcn);

        // Required to use getAssociationMappings method
        if (!$parentClassMetadata instanceof ClassMetadataInfo) {
            return;
        }

        try {
            $entityFqcn = $parentClassMetadata->getAssociationTargetClass($parentProperty);
        } catch (\Exception $e) {
            return;
        }

        return $entityFqcn;
    }

    /**
     * Returns EasyAdmin entity entry name for a FQCN.
     *
     * @param string $entityFqcn
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    public function guessEntityEntry(string $entityFqcn)
    {
        $matchingEntityConfigs = \array_filter(
            $this->easyAdminConfig['entities'],
            function ($entityConfig) use ($entityFqcn) {
                return $entityFqcn === $entityConfig['class'];
            }
        );

        if (empty($matchingEntityConfigs)) {
            throw new \RuntimeException(
                \sprintf('No entity defined in EasyAdmin configuration matches %s FQCN.', $entityFqcn)
            );
        }

        if (1 < \count($matchingEntityConfigs)) {
            throw new \RuntimeException(
                \sprintf(
                    'More than 1 entity defined in EasyAdmin configuration matches %s FQCN.'.
                    ' Try setting option "entity" (in type_options for NEW/EDIT/FORM view, or template_options for SHOW view)'.
                    ' with one of [%s].',
                    $entityFqcn,
                    \implode(', ', \array_keys($matchingEntityConfigs))
                )
{'fraction_non_alphanumeric': 0.09797599856707863, 'fraction_numerical': 0.0010746910263299302, 'mean_word_length': 2.1300448430493275, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '1059673', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1479, 'n_words': 360}