<commit_before><commit_msg>Add SynchronizeCommand<commit_after><?php

namespace ConnectHolland\TulipAPIBundle\Command;

use ConnectHolland\TulipAPIBundle\Model\TulipObjectInterface;
use ConnectHolland\TulipAPIBundle\Queue\QueueManager;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Synchronizes objects to Tulip.
 *
 * @author <NAME> <<EMAIL>>
 */
class SynchronizeCommand extends ContainerAwareCommand
{
    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        $this
            ->setName('tulip:synchronize')
            ->setDescription('Synchronizes objects to Tulip.');
    }

    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);

        $url = $this->getContainer()
            ->getParameter('tulip_api.url');

        $io->section('Preparing synchronization of all objects to: '.$url);

        $classMetaDataInstances = $this->getContainer()
            ->get('doctrine')
            ->getManager()
            ->getMetadataFactory()
            ->getAllMetadata();

        foreach ($classMetaDataInstances as $classMetaDataInstance) {
            if (is_subclass_of($classMetaDataInstance->getName(), TulipObjectInterface::class)) {
                $io->title(sprintf('Synchronizing "%s" objects.', $classMetaDataInstance->getName()));

                $queueManager = $this->getContainer()
                    ->get('tulip_api.queue_manager');
                /* @var $queueManager QueueManager */

                $entityManager = $this->getContainer()
                    ->get('doctrine')
                    ->getManager();
                /* @var $entityManager EntityManager */
                $count = $entityManager->createQueryBuilder()
                    ->select('count(o.id)')
                    ->from($classMetaDataInstance->getName(), 'o')
                    ->getQuery()
                    ->getSingleScalarResult();

                $query = $entityManager->createQueryBuilder()
                    ->select('o')
                    ->from($classMetaDataInstance->getName(), 'o')
                    ->getQuery();

                $io->progressStart($count);

                $result = $query->iterate();
                foreach ($result as $row) {
                    $queueManager->queueObject($row[0]);
                    $queueManager->sendQueue($entityManager);

                    $io->progressAdvance();
                }

                $io->progressFinish();
            }
        }

        $io->success('Synchronized all objects.');
    }
}
{'fraction_non_alphanumeric': 0.13153279551034724, 'fraction_numerical': 0.00035075412136092597, 'mean_word_length': 2.0732758620689653, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 42, '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': '2464297', 'n_tokens_mistral': 758, 'n_tokens_neox': 720, 'n_words': 117}