<commit_before><?php

/*
 * This file is part of the Sylius package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sylius\Bundle\CoreBundle\Fixture;

use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Webmozart\Assert\Assert;

/**
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractResourceFixture implements FixtureInterface
{
    /**
     * @var ObjectManager
     */
    private $resourceManager;

    /**
     * @var string
     */
    private $nodeName;

    /**
     * @var string|null
     */
    private $identifierField;

    /**
     * @param ObjectManager $resourceManager
     * @param string $nodeName
     * @param string|null $identifierField
     */
    public function __construct(ObjectManager $resourceManager, $nodeName, $identifierField = null)
    {
        $this->resourceManager = $resourceManager;
        $this->nodeName = $nodeName;
        $this->identifierField = $identifierField;
    }

    /**
     * @param array $options
     */
    final public function load(array $options)
    {
        $optionsResolver = $this->createConfiguredOptionsResolver($options);

        $resourcesOptions = array_merge($options[$this->nodeName], $this->generateResourcesOptions($options['random']));
        foreach ($resourcesOptions as $resourceOptions) {
            $resource = $this->loadResource($optionsResolver->resolve($resourceOptions));

            $this->resourceManager->persist($resource);
        }

        $this->resourceManager->flush();
    }

    /**
     * {@inheritdoc}
     */
    final public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $optionsNode = $treeBuilder->root($this->getName());

        $optionsNode->children()->integerNode('random')->min(0)->defaultValue(0);

        /** @var ArrayNodeDefinition $resourcesNode */
        $resourcesNode = $optionsNode->children()->arrayNode($this->nodeName);

        /** @var ArrayNodeDefinition $resourceNode */
        $resourceNode = $resourcesNode
            ->requiresAtLeastOneElement()
            ->prototype('array')
        ;

        if (null !== $this->identifierField) {
            $resourceNode
                ->children()
                ->scalarNode($this->identifierField)
                ->isRequired()
                ->cannotBeEmpty()<commit_msg>[Core] Improve performance a little bit<commit_after><?php

/*
 * This file is part of the Sylius package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sylius\Bundle\CoreBundle\Fixture;

use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Webmozart\Assert\Assert;

/**
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractResourceFixture implements FixtureInterface
{
    /**
     * @var ObjectManager
     */
    private $resourceManager;

    /**
     * @var string
     */
    private $nodeName;

    /**
     * @var string|null
     */
    private $identifierField;

    /**
     * @param ObjectManager $resourceManager
     * @param string $nodeName
     * @param string|null $identifierField
     */
    public function __construct(ObjectManager $resourceManager, $nodeName, $identifierField = null)
    {
        $this->resourceManager = $resourceManager;
        $this->nodeName = $nodeName;
        $this->identifierField = $identifierField;
    }

    /**
     * @param array $options
     */
    final public function load(array $options)
    {
        $optionsResolver = $this->createConfiguredOptionsResolver($options);

        $i = 0;
        $resourcesOptions = array_merge($options[$this->nodeName], $this->generateResourcesOptions($options['random']));
        foreach ($resourcesOptions as $resourceOptions) {
            $resource = $this->loadResource($optionsResolver->resolve($resourceOptions));

            $this->resourceManager->persist($resource);

            ++$i;

            if (0 === ($i % 10)) {
                $this->resourceManager->flush();
                $this->resourceManager->clear();
            }
        }

        $this->resourceManager->flush();
        $this->resourceManager->clear();
    }

    /**
     * {@inheritdoc}
     */
    final public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $optionsNode = $treeBuilder->root($this->getName());

        $optionsNode->children()->integerNode('random')->min(0)->defaultValue(0);

        /** @var ArrayNodeDefinition $resourcesNode */
        $resourcesNode = $optionsNode->children()->arrayNode($this->nodeName);

        /** @var ArrayNodeDefinition $resourceNode */
        $resourceNode = $resourcesNode
            ->requiresAtLeastOneElement()
            ->prototype('array')
        ;

        if (null !== $this->identifierField) {
            $resourceNode
                ->children()
                ->scalarNode($this->identifierField)
                ->isRequired()
                ->cannotBeEmpty()
{'fraction_non_alphanumeric': 0.12707365627073655, 'fraction_numerical': 0.0013271400132714001, 'mean_word_length': 2.902265372168285, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, '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': '3955788', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1562, 'n_words': 306}