<commit_before><?php

namespace NS\ImportBundle\Form;

use \Doctrine\Common\Persistence\ObjectManager;
use \Symfony\Component\Form\AbstractType;
use \Symfony\Component\Form\FormBuilderInterface;
use \Symfony\Component\Form\FormEvent;
use \Symfony\Component\Form\FormEvents;
use \Symfony\Component\OptionsResolver\OptionsResolver;
use \NS\ImportBundle\Entity\Import;
use \Symfony\Component\HttpFoundation\File\File;
use \Vich\UploaderBundle\Mapping\PropertyMappingFactory;

/**
 * Description of ImportSelectType
 *
 * @author gnat
 */
class ImportSelectType extends AbstractType
{
    /* @var $entityMgr ObjectManager */
    private $entityMgr;

    private $factory;

    /**
     * @param ObjectManager $entityMgr
     */
    public function __construct(ObjectManager $entityMgr, PropertyMappingFactory $factory)
    {
        $this->entityMgr = $entityMgr;
        $this->factory = $factory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('map', 'entity', array(
                'class'         => 'NSImportBundle:Map',
                'placeholder'   => 'Please Select...',
                'query_builder' => $this->entityMgr->getRepository('NSImportBundle:Map')->getWithColumnsQuery(),
                )
            )
            ->add('sourceFile', 'vich_file',array('error_bubbling'=>false))
            ->add('import', 'submit', array('attr' => array('class' => 'btn btn-xs btn-success pull-right')))
        ;

        $builder->addEventListener(FormEvents::POST_SUBMIT,array($this,'postSubmit'));
    }

    public function postSubmit(FormEvent $event)
    {
        $import = $event->getData();
        $import->setDuplicateFile($this->createNewFile($import, 'duplicate-state.txt', 'duplicateFile'));
        $import->setMessageFile($this->createNewFile($import, 'messages.csv', 'messageFile'));
        $import->setWarningFile($this->createNewFile($import, 'warnings.csv', 'warningFile'));
        $import->setErrorFile($this->createNewFile($import, 'errors.csv', 'errorFile'));
        $import->setSuccessFile($this->createNewFile($import, 'successes.csv', 'successFile'));
    }

    /**
     * @param Import $import
     * @param $name
     * @param $property
     * @return File
     */
    public function createNewFile(Import $import, $name, $property)
    {
        $file = new File(tempnam(sys_get_temp_dir(), 'import-output'));
        $mapping = $this->factory->fromField($import, $property);
        if($mapping) {
            $mapping->setFileName($import, $name);

            // determine the file's directory
            $dir = $mapping->getUploadDir($import);

            $uploadDir = $mapping->getUploadDestination() . DIRECTORY_SEPARATOR . $dir;
<commit_msg>added docblock<commit_after><?php

namespace NS\ImportBundle\Form;

use \Doctrine\Common\Persistence\ObjectManager;
use \Symfony\Component\Form\AbstractType;
use \Symfony\Component\Form\FormBuilderInterface;
use \Symfony\Component\Form\FormEvent;
use \Symfony\Component\Form\FormEvents;
use \Symfony\Component\OptionsResolver\OptionsResolver;
use \NS\ImportBundle\Entity\Import;
use \Symfony\Component\HttpFoundation\File\File;
use \Vich\UploaderBundle\Mapping\PropertyMappingFactory;

/**
 * Description of ImportSelectType
 *
 * @author gnat
 */
class ImportSelectType extends AbstractType
{
    /* @var $entityMgr ObjectManager */
    private $entityMgr;

    private $factory;

    /**
     * @param ObjectManager $entityMgr
     */
    public function __construct(ObjectManager $entityMgr, PropertyMappingFactory $factory)
    {
        $this->entityMgr = $entityMgr;
        $this->factory = $factory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('map', 'entity', array(
                'class'         => 'NSImportBundle:Map',
                'placeholder'   => 'Please Select...',
                'query_builder' => $this->entityMgr->getRepository('NSImportBundle:Map')->getWithColumnsQuery(),
                )
            )
            ->add('sourceFile', 'vich_file',array('error_bubbling'=>false))
            ->add('import', 'submit', array('attr' => array('class' => 'btn btn-xs btn-success pull-right')))
        ;

        $builder->addEventListener(FormEvents::POST_SUBMIT,array($this,'postSubmit'));
    }

    /**
     * @param FormEvent $event
     */
    public function postSubmit(FormEvent $event)
    {
        $import = $event->getData();
        $import->setDuplicateFile($this->createNewFile($import, 'duplicate-state.txt', 'duplicateFile'));
        $import->setMessageFile($this->createNewFile($import, 'messages.csv', 'messageFile'));
        $import->setWarningFile($this->createNewFile($import, 'warnings.csv', 'warningFile'));
        $import->setErrorFile($this->createNewFile($import, 'errors.csv', 'errorFile'));
        $import->setSuccessFile($this->createNewFile($import, 'successes.csv', 'successFile'));
    }

    /**
     * @param Import $import
     * @param $name
     * @param $property
     * @return File
     */
    public function createNewFile(Import $import, $name, $property)
    {
        $file = new File(tempnam(sys_get_temp_dir(), 'import-output'));
        $mapping = $this->factory->fromField($import, $property);
        if($mapping) {
            $mapping->setFileName($import, $name);

            // determine the file's directory
            $dir = $mapping->getUploadDir($import);

            $uploadDir = $mapping->getUploadDestination() . DIRECTORY_SEPARATOR . $dir;

{'fraction_non_alphanumeric': 0.14987800627396305, 'fraction_numerical': 0.0, 'mean_word_length': 3.1647314949201744, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '1360851', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1639, 'n_words': 274}