<commit_before><?php
/**
 * @copyright 2014 Integ S.A.
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
 * @author <NAME> <<EMAIL>>
 */

namespace GoIntegro\Hateoas\Config;

// ORM.
use Doctrine\ORM\EntityManagerInterface;
// Metadata.
use GoIntegro\Hateoas\Metadata\Entity\MetadataCache;
// RAML.
use GoIntegro\Raml\DocNavigator;
// Utils.
use GoIntegro\Hateoas\Util;

class ResourceEntityMapper
{
    const RESOURCE_ENTITY_INTERFACE = 'GoIntegro\\Hateoas\\JsonApi\\ResourceEntityInterface';

    const ERROR_MISSING_ENTITY = "No entity matches the resource \"%s\".",
        ERROR_ENTITIES_PER_RESOURCE = "The resource \"%s\" listed in the RAML doc matches the following entity class names: \"%s\". If you want to keep the short-names of these resource entities you need to map all but one of them to other resource types in the bundle configuration.";

    /**
     * @var EntityManagerInterface
     */
    private $em;
    /**
     * @var MetadataCache
     */
    private $metadataCache;
    /**
     * @var DocNavigator
     */
    private $docNavigator;
    /**
     * @var ResourceEntityMapperCache
     */
    private $mapCache;
    /**
     * @var array
     */
    private $indexedClassNames;

    /**
     * @param EntityManagerInterface $em
     * @param MetadataCache $metadataCache
     * @param DocNavigator $docNavigator
     * @param ResourceEntityMapperCache $mapCache
     */
    public function __construct(
        EntityManagerInterface $em,
        MetadataCache $metadataCache,
        DocNavigator $docNavigator,
        ResourceEntityMapperCache $mapCache
    )
    {
        $this->em = $em;
        $this->metadataCache = $metadataCache;
        $this->docNavigator = $docNavigator;
        $this->mapCache = $mapCache;
        $this->indexedClassNames = $this->indexEntityClassNames();
    }

    /**
     * @return array
     */
    private function indexEntityClassNames()
    {
        $indexedClassNames = [];
        $entityClassNames = $this->em->getConfiguration()
            ->getMetadataDriverImpl()
            ->getAllClassNames();

        foreach ($entityClassNames as $name) {
            // @todo Support subtypes.
            $resourceType = Util\Inflector::typify($name);
            $indexedClassNames[$resourceType][] = $name;
        }

        return $indexedClassNames;
    }

    /**
     * @return array
     * @throws ApiDefinitionException<commit_msg>Map, not mapper.<commit_after><?php
/**
 * @copyright 2014 Integ S.A.
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
 * @author <NAME> <<EMAIL>>
 */

namespace GoIntegro\Hateoas\Config;

// ORM.
use Doctrine\ORM\EntityManagerInterface;
// Metadata.
use GoIntegro\Hateoas\Metadata\Entity\MetadataCache;
// RAML.
use GoIntegro\Raml\DocNavigator;
// Utils.
use GoIntegro\Hateoas\Util;

class ResourceEntityMapper
{
    const RESOURCE_ENTITY_INTERFACE = 'GoIntegro\\Hateoas\\JsonApi\\ResourceEntityInterface';

    const ERROR_MISSING_ENTITY = "No entity matches the resource \"%s\".",
        ERROR_ENTITIES_PER_RESOURCE = "The resource \"%s\" listed in the RAML doc matches the following entity class names: \"%s\". If you want to keep the short-names of these resource entities you need to map all but one of them to other resource types in the bundle configuration.";

    /**
     * @var EntityManagerInterface
     */
    private $em;
    /**
     * @var MetadataCache
     */
    private $metadataCache;
    /**
     * @var DocNavigator
     */
    private $docNavigator;
    /**
     * @var ResourceEntityMapCache
     */
    private $mapCache;
    /**
     * @var array
     */
    private $indexedClassNames;

    /**
     * @param EntityManagerInterface $em
     * @param MetadataCache $metadataCache
     * @param DocNavigator $docNavigator
     * @param ResourceEntityMapCache $mapCache
     */
    public function __construct(
        EntityManagerInterface $em,
        MetadataCache $metadataCache,
        DocNavigator $docNavigator,
        ResourceEntityMapCache $mapCache
    )
    {
        $this->em = $em;
        $this->metadataCache = $metadataCache;
        $this->docNavigator = $docNavigator;
        $this->mapCache = $mapCache;
        $this->indexedClassNames = $this->indexEntityClassNames();
    }

    /**
     * @return array
     */
    private function indexEntityClassNames()
    {
        $indexedClassNames = [];
        $entityClassNames = $this->em->getConfiguration()
            ->getMetadataDriverImpl()
            ->getAllClassNames();

        foreach ($entityClassNames as $name) {
            // @todo Support subtypes.
            $resourceType = Util\Inflector::typify($name);
            $indexedClassNames[$resourceType][] = $name;
        }

        return $indexedClassNames;
    }

    /**
     * @return array
     * @throws ApiDefinitionException
{'fraction_non_alphanumeric': 0.11500412201154163, 'fraction_numerical': 0.0016488046166529267, 'mean_word_length': 2.573637702503682, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '5300800', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1402, 'n_words': 331}