<commit_before><commit_msg>add command to update to using rounds<commit_after><?php

namespace CompetitionBundle\Command;

use CompetitionBundle\Entity\Match;
use CompetitionBundle\Entity\Round;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * CompetitionBundle\Command\UpdateToRoundsCommand
 *
 * @author <NAME> <<EMAIL>>
 */
class UpdateToRoundsCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('app:update-to-rounds')
            ->setDescription('Update all matches to use rounds')
            ->setHelp('Update all matches to use rounds');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /** @var EntityManager $em */
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');

        $matches = $em->getRepository(Match::class)->findBy([
            'round' => null,
        ]);

        /** @var Match $match */
        foreach ($matches as $match) {
            $formattedDate = $match->getDate()->format('Ymd');

            /** @var Round $round */
            if (!$round = $em->getRepository(Round::class)->findOneByName($formattedDate)) {
                $round = $this->createRound($formattedDate);
                $em->persist($round);
                $em->flush();
                $output->writeln(sprintf('<info>Created new round %s</info>', $formattedDate));
            }

            // add match and its players to the round
            $round->addMatch($match);
            $round->addPlayer($match->getHomePlayer());
            $round->addPlayer($match->getAwayPlayer());

            $em->flush();
        }

        return 0;
    }

    /**
     * Creates a new round
     *
     * @param $dateString
     * @return Round
     */
    private function createRound($dateString)
    {
        $round = new Round();
        $round->setName($dateString);

        return $round;
    }
}
{'fraction_non_alphanumeric': 0.14279155188246098, 'fraction_numerical': 0.0004591368227731864, 'mean_word_length': 2.3471582181259603, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, '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': '4226274', 'n_tokens_mistral': 602, 'n_tokens_neox': 587, 'n_words': 118}