<commit_before><?php

namespace Ilios\CoreBundle\DataFixtures\ORM;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Common\DataFixtures\AbstractFixture as DataFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Ilios\CoreBundle\Traits\IdentifiableEntityInterface;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A Doctrine fixture based data loader base class that populates the DB from data files.
 *
 * Class AbstractFixture
 * @package Ilios\CoreBundle\DataFixtures\ORM
 */
abstract class AbstractFixture extends DataFixture implements
    FixtureInterface,
    ContainerAwareInterface
{
    /**
     * @var string doubles as identifier for this fixture's data file and entity references.
     */
    protected $key;

    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * @param string $key
     */
    public function __construct($key)
    {
        $this->key = $key;
    }

    public function getKey()
    {
        return $this->key;
    }

    /**
     * {@inheritdoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    /**
     * {@inheritdoc}
     */
    public function load(ObjectManager $manager)
    {

        $fileName = $this->getKey() . '.csv';
        $path = $this->getDataFilePath($fileName);

        $first = true;
        if (($handle = fopen($path, 'r')) !== FALSE) {
            while (($data = fgetcsv($handle)) !== FALSE) {
                // step over the first row
                // since it contains the field names
                if ($first) {
                    $first = false;
                    continue;
                }
                $entity = $this->createEntity($data);
                $manager->persist($entity);

                // honor the given entity identifiers.
                // @link http://www.ens.ro/2012/07/03/symfony2-doctrine-force-entity-id-on-persist/
                $metadata = $manager->getClassMetaData(get_class($entity));
                $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);

                $this->addReference($this->getKey() . $entity->getId(), $entity);

                $manager->flush();
            }
            fclose($handle);
        }
    }

    /**
     * Finds and return the absolute path to a given data file.
     * @param string $fileName name of the data file.
     * @return string the absolute path.
     */
    protected function getDataFilePath($fileName)
    {
        /**
         * @var FileLocator $fileLocator
         */<commit_msg>added ability to conditionally store references to fixture data during loading process.<commit_after><?php

namespace Ilios\CoreBundle\DataFixtures\ORM;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Common\DataFixtures\AbstractFixture as DataFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Ilios\CoreBundle\Traits\IdentifiableEntityInterface;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A Doctrine fixture based data loader base class that populates the DB from data files.
 *
 * Class AbstractFixture
 * @package Ilios\CoreBundle\DataFixtures\ORM
 */
abstract class AbstractFixture extends DataFixture implements
    FixtureInterface,
    ContainerAwareInterface
{
    /**
     * @var string
     * Doubles as identifier for this fixture's data file and entity references.
     */
    protected $key;

    /**
     * @var boolean
     * Set to TRUE if the loaded fixture should be held on for reference.
     */
    protected $storeReference;

    /**
     * @var ContainerInterface
     */
    private $container;



    /**
     * @param string $key
     * @param boolean $storeReference
     */
    public function __construct($key, $storeReference = true)
    {
        $this->key = $key;
        $this->storeReference = $storeReference;
    }

    public function getKey()
    {
        return $this->key;
    }

    /**
     * {@inheritdoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    /**
     * {@inheritdoc}
     */
    public function load(ObjectManager $manager)
    {

        $fileName = $this->getKey() . '.csv';
        $path = $this->getDataFilePath($fileName);

        $first = true;
        if (($handle = fopen($path, 'r')) !== FALSE) {
            while (($data = fgetcsv($handle)) !== FALSE) {
                // step over the first row
                // since it contains the field names
                if ($first) {
                    $first = false;
                    continue;
                }
                $entity = $this->createEntity($data);
                $manager->persist($entity);

                // honor the given entity identifiers.
                // @link http://www.ens.ro/2012/07/03/symfony2-doctrine-force-entity-id-on-persist/
                $metadata = $manager->getClassMetaData(get_class($entity));
                $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);

                if ($this->storeReference) {
                    $this->addReference($this->getKey() . $entity->getId(), $entity);
                }
                $manager->flush();
            }
            fclose($handle);
        }
    }

    /**
     * Finds and return the absolute path to a given data file.
     * @param string $fileName name of the data file.
     * @return string the absolute path.
     */
    protected function getDataFilePath($fileName)
    {
        /**
         * @var FileLocator $fileLocator
         */
{'fraction_non_alphanumeric': 0.11248744559758955, 'fraction_numerical': 0.003013056578506863, 'mean_word_length': 2.183271177410762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1561092', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1590, 'n_words': 383}