<commit_before><?php

namespace Hangman\Bundle\ApiBundle\Controller;

// @todo optimize imports throughout app

use FOS\RestBundle\View\View;
use Hangman\Bundle\ApiBundle\Exception\InvalidTokenException;
use Hangman\Bundle\DatastoreBundle\Entity\ORM\Game;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class GameController
 *
 * @package Hangman\Bundle\ApiBundle\Controller
 */
class GameController extends Controller
{
    /**
     * Simple authentication mechanism.
     *
     * Normally, on more elaborate APIs, we would want to use an implementation
     * of Symfony's SimplePreAuthenticatorInterface. However, given the
     * simplicity of this API, this method suffices for now.
     *
     * @param Request $request
     *
     * @return bool
     */
    protected function authenticate(Request $request)
    {
        $token = $request->headers->get('X-Hangman-Token');
        $consumer = $this->getDoctrine()->getManager()
            ->getRepository('HangmanDatastoreBundle:ORM\Consumer')
            ->findByToken($token);

        if (empty($consumer)) {
            throw new InvalidTokenException(
                403, 'Invalid authentication token.'
            );
        }
    }

    /**
     * @param Request $request
     *
     * @return View
     */
    public function postGamesAction(Request $request)
    {
        $view = View::create();

        $this->authenticate($request);

        $em = $this->getDoctrine()->getManager();

        $word = $em
            ->getRepository('HangmanDatastoreBundle:ORM\Word')
            ->getRandomWord();

        $game = new Game();
        $game->setWord($word);

        $em->persist($game);
        $em->flush();

        return $view->setData($game);
    }

    /**
     * @param Request $request
     * @param int     $id
     *
     * @return mixed
     */
    public function putGameAction(Request $request, $id)
    {
        $this->authenticate($request);

        $game = $this->getDoctrine()->getManager()
            ->getRepository('HangmanDatastoreBundle:ORM\Game')
            ->find($id);

        $character = $request->request->get('character');

        $game = $this
            ->get('hangman_api.processor')
            ->process($game, $character);

        return View::create($game, 200);
    }
}
<commit_msg>[Empty commit message]<commit_after><?php

namespace Hangman\Bundle\ApiBundle\Controller;

use FOS\RestBundle\View\View;
use Hangman\Bundle\ApiBundle\Exception\InvalidTokenException;
use Hangman\Bundle\DatastoreBundle\Entity\ORM\Game;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class GameController
 *
 * @package Hangman\Bundle\ApiBundle\Controller
 */
class GameController extends Controller
{
    /**
     * Simple authentication mechanism.
     *
     * Normally, on more elaborate APIs, we would want to use an implementation
     * of Symfony's SimplePreAuthenticatorInterface. However, given the
     * simplicity of this API, this method suffices for now.
     *
     * @param Request $request
     *
     * @return bool
     */
    protected function authenticate(Request $request)
    {
        $token = $request->headers->get('X-Hangman-Token');
        $consumer = $this->getDoctrine()->getManager()
            ->getRepository('HangmanDatastoreBundle:ORM\Consumer')
            ->findByToken($token);

        if (empty($consumer)) {
            throw new InvalidTokenException(
                403, 'Invalid authentication token.'
            );
        }
    }

    /**
     * @param Request $request
     *
     * @return View
     */
    public function postGamesAction(Request $request)
    {
        $view = View::create();

        $this->authenticate($request);

        $em = $this->getDoctrine()->getManager();

        $word = $em
            ->getRepository('HangmanDatastoreBundle:ORM\Word')
            ->getRandomWord();

        $game = new Game();
        $game->setWord($word);

        $em->persist($game);
        $em->flush();

        return $view->setData($game);
    }

    /**
     * @param Request $request
     * @param int     $id
     *
     * @return mixed
     */
    public function putGameAction(Request $request, $id)
    {
        $this->authenticate($request);

        $game = $this->getDoctrine()->getManager()
            ->getRepository('HangmanDatastoreBundle:ORM\Game')
            ->find($id);

        $character = $request->request->get('character');

        $game = $this
            ->get('hangman_api.processor')
            ->process($game, $character);

        return View::create($game, 200);
    }
}

{'fraction_non_alphanumeric': 0.13283740701381508, 'fraction_numerical': 0.0025504782146652497, 'mean_word_length': 2.3807471264367814, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '978310', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1282, 'n_words': 265}