<commit_before><?php
/*
 * This file is part of the Sidus/EAVModelBundle package.
 *
 * Copyright (c) 2015-2018 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sidus\EAVModelBundle\Request\ParamConverter;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sidus\BaseBundle\Request\ParamConverter\AbstractParamConverter;
use Sidus\EAVModelBundle\Doctrine\DataLoaderInterface;
use Sidus\EAVModelBundle\Entity\DataInterface;
use Sidus\EAVModelBundle\Entity\DataRepository;
use Sidus\EAVModelBundle\Registry\FamilyRegistry;
use Symfony\Component\HttpFoundation\Request;

/**
 * Convert request parameters in data
 *
 * @author <NAME> <<EMAIL>>
 */
class DataParamConverter extends AbstractParamConverter
{
    /** @var DataRepository */
    protected $repository;

    /** @var FamilyRegistry */
    protected $familyRegistry;

    /** @var DataLoaderInterface */
    protected $dataLoader;

    /**
     * @param FamilyRegistry         $familyRegistry
     * @param DataLoaderInterface    $dataLoader
     * @param EntityManagerInterface $entityManager
     * @param string                 $dataClass
     */
    public function __construct(
        FamilyRegistry $familyRegistry,
        DataLoaderInterface $dataLoader,
        EntityManagerInterface $entityManager,
        $dataClass
    ) {
        $this->familyRegistry = $familyRegistry;
        $this->dataLoader = $dataLoader;
        $this->repository = $entityManager->getRepository($dataClass);
    }

    /**
     * @param int|string     $value
     * @param ParamConverter $configuration
     *
     * @throws \Doctrine\ORM\NonUniqueResultException
     * @throws \Sidus\EAVModelBundle\Exception\MissingFamilyException
     * @throws \UnexpectedValueException
     * @throws \LogicException
     * @throws ORMException
     *
     * @return null|DataInterface
     */
    protected function convertValue($value, ParamConverter $configuration)
    {
        if (array_key_exists('family', $configuration->getOptions())) {
            $family = $this->familyRegistry->getFamily($configuration->getOptions()['family']);
            if ($family->isSingleton()) {
                $data = $this->repository->getInstance($family);
            } else {
                $data = $this->repository->findByIdentifier($family, $value, true);
            }
        } else {
            $data = $this->repository->find($value);
        }
        $this->dataLoader->loadSingle($data, $data->getFamily()->getOption('loader_depth', 2));

        return $data;
    }

    /**
     * @return mixed
     */
    protected function getClass()
    {
        return DataInterface::class;
    }<commit_msg>Fixing Data param converter handling null result<commit_after><?php
/*
 * This file is part of the Sidus/EAVModelBundle package.
 *
 * Copyright (c) 2015-2018 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sidus\EAVModelBundle\Request\ParamConverter;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sidus\BaseBundle\Request\ParamConverter\AbstractParamConverter;
use Sidus\EAVModelBundle\Doctrine\DataLoaderInterface;
use Sidus\EAVModelBundle\Entity\DataInterface;
use Sidus\EAVModelBundle\Entity\DataRepository;
use Sidus\EAVModelBundle\Registry\FamilyRegistry;
use Symfony\Component\HttpFoundation\Request;

/**
 * Convert request parameters in data
 *
 * @author <NAME> <<EMAIL>>
 */
class DataParamConverter extends AbstractParamConverter
{
    /** @var DataRepository */
    protected $repository;

    /** @var FamilyRegistry */
    protected $familyRegistry;

    /** @var DataLoaderInterface */
    protected $dataLoader;

    /**
     * @param FamilyRegistry         $familyRegistry
     * @param DataLoaderInterface    $dataLoader
     * @param EntityManagerInterface $entityManager
     * @param string                 $dataClass
     */
    public function __construct(
        FamilyRegistry $familyRegistry,
        DataLoaderInterface $dataLoader,
        EntityManagerInterface $entityManager,
        $dataClass
    ) {
        $this->familyRegistry = $familyRegistry;
        $this->dataLoader = $dataLoader;
        $this->repository = $entityManager->getRepository($dataClass);
    }

    /**
     * @param int|string     $value
     * @param ParamConverter $configuration
     *
     * @throws \Doctrine\ORM\NonUniqueResultException
     * @throws \Sidus\EAVModelBundle\Exception\MissingFamilyException
     * @throws \UnexpectedValueException
     * @throws \LogicException
     * @throws ORMException
     *
     * @return null|DataInterface
     */
    protected function convertValue($value, ParamConverter $configuration)
    {
        if (array_key_exists('family', $configuration->getOptions())) {
            $family = $this->familyRegistry->getFamily($configuration->getOptions()['family']);
            if ($family->isSingleton()) {
                $data = $this->repository->getInstance($family);
            } else {
                $data = $this->repository->findByIdentifier($family, $value, true);
            }
        } else {
            $data = $this->repository->find($value);
        }
        if ($data) {
            $this->dataLoader->loadSingle($data, $data->getFamily()->getOption('loader_depth', 2));
        }

        return $data;
    }

    /**
     * @return mixed
     */
    protected function getClass()
    {
        return DataInterface::class;
    }
{'fraction_non_alphanumeric': 0.11143949930458971, 'fraction_numerical': 0.003129346314325452, 'mean_word_length': 2.9923664122137406, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 49, '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': '3944980', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1502, 'n_words': 320}