<commit_before><?php
/**
 * /src/App/Controllers/RestController.php
 *
 * @author  TLe, <NAME> <<EMAIL>>
 */
namespace App\Controllers;

// Application components
use App\Services\Rest as RestService;

// Silex components
use Silex\Application;

// Symfony components
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Validator\Exception\ValidatorException;

/**
 * Abstract class that all basic REST controllers uses.
 *
 * @category    Controller
 * @package     App\Controllers
 * @author      TLe, <NAME> <<EMAIL>>
 */
abstract class Rest extends Base implements Interfaces\Rest
{
    /**
     * Service object for current REST controller.
     *
     * @var RestService
     */
    protected $service;

    /**
     * Returns routes to connect to the given application.
     *
     * @param   Application $app
     *
     * @return  \Silex\ControllerCollection
     */
    public function connect(Application $app)
    {
        parent::connect($app);

        $this->exposeServices();

        return $this->controllers;
    }

    /**
     * Returns all specified entities from database as an array of entity objects.
     *
     * @return  JsonResponse
     */
    public function find()
    {
        return $this->app->json($this->service->find());
    }

    /**
     * Returns single entity object presenting given id value.
     *
     * @param   integer $id Entity id
     *
     * @return  JsonResponse
     */
    public function findOne($id)
    {
        $entity = $this->service->findOne($id);

        if (is_null($entity)) {
            throw new HttpException(404, 'Not found');
        }

        return $this->app->json($entity);
    }

    /**
     * Creates new entity object to the database.
     *
     * @param   Request $request
     *
     * @return  JsonResponse
     */
    public function create(Request $request)
    {
        $data = json_decode($request->getContent());

        try {
            $entity = $this->service->create($data);
        } catch (ValidatorException $error) {
            throw new HttpException(400, $error->getMessage());
        }

        return $this->app->json($entity, 201);
    }

    /**
     * Updates specified entity data to database.
     *
     * @param   Request $request
     * @param   integer $id
     *
     * @return  JsonResponse
     */
    public function update(Request $request, $id)
    {
        $data = json_decode($request->getContent());

        try {
            $entity = $this->service->update($id, $data);
        } catch (ValidatorException $error) {
            throw new HttpException(400, $error->getMessage());
        }

        return $this->app->json($entity, 200);
    }

    /**
     * Method to delete specified entity from database.
     *
     * @param   integer $id
     *
     * @return  JsonResponse
     */
    public function delete($id)
    {
        $this->service->delete($id);

        return $this->app->json('', 204);<commit_msg>Refactored REST controller.<commit_after><?php
/**
 * /src/App/Controllers/RestController.php
 *
 * @author  TLe, <NAME> <<EMAIL>>
 */
namespace App\Controllers;

// Application components
use App\Services\Rest as RestService;
use App\Entities\Base as BaseEntity;

// Silex components
use Silex\Application;

// Symfony components
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Validator\Exception\ValidatorException;

/**
 * Abstract class that all basic REST controllers uses.
 *
 * @category    Controller
 * @package     App\Controllers
 * @author      TLe, <NAME> <<EMAIL>>
 */
abstract class Rest extends Base implements Interfaces\Rest
{
    /**
     * Service object for current REST controller.
     *
     * @var RestService
     */
    protected $service;

    /**
     * Returns routes to connect to the given application.
     *
     * @param   Application $app
     *
     * @return  \Silex\ControllerCollection
     */
    public function connect(Application $app)
    {
        parent::connect($app);

        $this->exposeServices();

        return $this->controllers;
    }

    /**
     * Returns all specified entities from database as an array of entity objects.
     *
     * @return  Response
     */
    public function find()
    {
        return $this->makeResponse($this->service->find());
    }

    /**
     * Returns single entity object presenting given id value.
     *
     * @param   integer $id Entity id
     *
     * @return  Response
     */
    public function findOne($id)
    {
        $entity = $this->service->findOne($id);

        if (is_null($entity)) {
            throw new HttpException(404, 'Not found');
        }

        return $this->makeResponse($entity);
    }

    /**
     * Creates new entity object to the database.
     *
     * @param   Request $request
     *
     * @return  Response
     */
    public function create(Request $request)
    {
        $data = json_decode($request->getContent());

        try {
            $entity = $this->service->create($data);
        } catch (ValidatorException $error) {
            throw new HttpException(400, $error->getMessage());
        }

        return $this->makeResponse($entity, 201);
    }

    /**
     * Updates specified entity data to database.
     *
     * @param   Request $request
     * @param   integer $id
     *
     * @return  Response
     */
    public function update(Request $request, $id)
    {
        $data = json_decode($request->getContent());

        try {
            $entity = $this->service->update($id, $data);
        } catch (ValidatorException $error) {
            throw new HttpException(400, $error->getMessage());
        }

        return $this->makeResponse($entity, 200);
    }

    /**
     * Method to delete specified entity from database.
     *
     * @param   integer $id
     *
     * @return  Response
     */
    public function delete($id)
    {
        $this->service->delete($id);

        return $this->makeResponse('', 204);
    }

    /**
     * Helper method to make JSON response.
     *
     * @param   null|string|BaseEntity|BaseEntity[] $data
     * @param   integer                             $statusCode
     *
     * @return  Response
     */
    protected function makeResponse($data, $statusCode = 200)
    {
        // Create new response
        $response = new Response();
        $response->setContent(
            (empty($data) && !is_array($data)) ? '' : $this->app['serializer']->serialize($data, 'json')
        );
        $response->setStatusCode($statusCode);
        $response->headers->set('Content-Type', 'application/json');

        return $response;
{'fraction_non_alphanumeric': 0.12662479918212355, 'fraction_numerical': 0.005695925222725281, 'mean_word_length': 2.186598417868776, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 68, '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': '4911391', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1883, 'n_words': 470}