<commit_before><?php

namespace Knp\ChallengeBundle;

use Guzzle\Http\Client;
use Symfony\Component\DomCrawler\Crawler;
use Knp\ChallengeBundle\Entity\Team;
use Knp\ChallengeBundle\Entity\Game;
use Doctrine\ORM\EntityManager;

class Helper
{
    private $em;

    public function __construct(EntityManager $entityManager)
    {
        $this->em = $entityManager;
    }

    public function fetchData()
    {
        // Walk through the pages
        for ($page = 0; $matchesTable = $this->getContent($page); $page++) {
            $crawler = new Crawler($matchesTable);

            // Walk through the table
            for ($tr=0; $tr < $crawler->filter('tbody > tr')->count(); $tr++) {
                $game = new Game();

                // Get date
                $timestamp = $this->getTimestampFromTable($crawler, $tr);

                $date = date('Y-m-d', $timestamp);
                $date = new \DateTime($date);
                $game->setDate($date);

                // Home Team
                $homeTeamName = $this->getTeamFromTable($crawler, $tr, 2);
                $homeTeam = $this->em->getRepository('ChallengeBundle:Team')->findOneByName($homeTeamName);

                if (!$homeTeam) {
                    $homeTeam = new Team();
                    $homeTeam->setName($homeTeamName);
                    $this->em->persist($homeTeam);
                }

                $game->setHomeTeam($homeTeam);

                // Score
                $score = $this->getScoreFromTable($crawler, $tr);
                $scoreArray = explode('-', $score);
                $game->setHomeTeamScore(trim($scoreArray['0']));
                $game->setAwayTeamScore(trim($scoreArray['1']));

                // Away Team
                $awayTeamName = $this->getTeamFromTable($crawler, $tr, 4);
                $awayTeam = $this->em->getRepository('ChallengeBundle:Team')->findOneByName($awayTeamName);

                if (!$awayTeam) {
                    $awayTeam = new Team();
                    $awayTeam->setName($awayTeamName);
                    $this->em->persist($awayTeam);
                }

                $game->setAwayTeam($awayTeam);

                if (!$this->em->getRepository('ChallengeBundle:Game')->existThisGame($game)) {
                    $this->em->persist($game);
                }

                $this->em->flush();
            }
        }
        return true;
    }

    public function getContent($page)
    {
        if ($page != 0) {
            $page = '-'.$page;
        }<commit_msg>Created some helper methods for API<commit_after><?php

namespace Knp\ChallengeBundle;

use Guzzle\Http\Client;
use Symfony\Component\DomCrawler\Crawler;
use Knp\ChallengeBundle\Entity\Team;
use Knp\ChallengeBundle\Entity\Game;
use Doctrine\ORM\EntityManager;

class Helper
{
    private $em;

    public function __construct(EntityManager $entityManager)
    {
        $this->em = $entityManager;
    }

    public function fetchData()
    {
        // Walk through the pages
        for ($page = 0; $matchesTable = $this->getContent($page); $page++) {
            $crawler = new Crawler($matchesTable);

            // Walk through the table
            for ($tr=0; $tr < $crawler->filter('tbody > tr')->count(); $tr++) {
                $game = new Game();

                // Get date
                $timestamp = $this->getTimestampFromTable($crawler, $tr);

                $date = date('Y-m-d', $timestamp);
                $date = new \DateTime($date);
                $game->setDate($date);

                // Home Team
                $homeTeamName = $this->getTeamFromTable($crawler, $tr, 2);
                $homeTeam = $this->em->getRepository('ChallengeBundle:Team')->findOneByName($homeTeamName);

                if (!$homeTeam) {
                    $homeTeam = new Team();
                    $homeTeam->setName($homeTeamName);
                    $this->em->persist($homeTeam);
                }

                $game->setHomeTeam($homeTeam);

                // Score
                $score = $this->getScoreFromTable($crawler, $tr);
                $scoreArray = explode('-', $score);
                $game->setHomeTeamScore(trim($scoreArray['0']));
                $game->setAwayTeamScore(trim($scoreArray['1']));

                // Away Team
                $awayTeamName = $this->getTeamFromTable($crawler, $tr, 4);
                $awayTeam = $this->em->getRepository('ChallengeBundle:Team')->findOneByName($awayTeamName);

                if (!$awayTeam) {
                    $awayTeam = new Team();
                    $awayTeam->setName($awayTeamName);
                    $this->em->persist($awayTeam);
                }

                $game->setAwayTeam($awayTeam);

                if (!$this->em->getRepository('ChallengeBundle:Game')->existThisGame($game)) {
                    $this->em->persist($game);
                }

                $this->em->flush();
            }
        }
        return true;
    }

    public function cmpTeam(Team $a, Team $b)
    {
        $ap = $a->getPoints();
        $bp = $b->getPoints();
        if ($ap == $bp) {
            return 0;
        }
        return ($ap > $bp) ? -1 : +1;
    }

    public function getStandingsData(Team $team, $from, $to)
    {
        foreach ($team->getHomeTeamGames() as $game) {
            if ($game->getDateString() >= $from && $game->getDateString() <= $to) {
                if ($game->getHomeTeamScore() < $game->getAwayTeamScore()) {
                    $team->setLosses($team->getLosses() + 1);
                }
                elseif ($game->getHomeTeamScore() > $game->getAwayTeamScore()) {
                    $team->setWins($team->getWins() + 1);
                }
                else {
                    $team->setDraws($team->getDraws() + 1);
                }

                $team->setPlayed($team->getPlayed()+1);
            }
        }

        foreach ($team->getAwayTeamGames() as $game) {
            if ($game->getDateString() >= $from && $game->getDateString() <= $to) {
                if ($game->getAwayTeamScore() < $game->getHomeTeamScore()) {
                    $team->setLosses($team->getLosses() + 1);
                }
                elseif ($game->getAwayTeamScore() > $game->getHomeTeamScore()) {
                    $team->setWins($team->getWins() + 1);
                }
                else {
                    $team->setDraws($team->getDraws() + 1);
                }

                $team->setPlayed($team->getPlayed()+1);
            }
        }

        $team->considerPoints();

        return $team;
    }

    public function getContent($page)
    {
        if ($page != 0) {
            $page = '-'.$page;
        }
{'fraction_non_alphanumeric': 0.15459009076030353, 'fraction_numerical': 0.0037196845707484004, 'mean_word_length': 1.5953667953667954, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 107, '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': '2345062', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1862, 'n_words': 318}