<commit_before><?php

namespace Dime\TimetrackerBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\View\View;
use Dime\TimetrackerBundle\Controller\DimeController;
use Dime\TimetrackerBundle\Entity\Project;
use Dime\TimetrackerBundle\Form\ProjectType;

class ProjectController extends DimeController
{
    /**
     * get project repository
     *
     * @return Dime\TimetrackerBundle\Entity\ProjectRepository
     */
    protected function getProjectRepository()
    {
        return $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Project');
    }

    /**
     * get a list of all projects
     *
     * [GET] /projects
     */
    public function getProjectsAction()
    {
        $projects = $this->getProjectRepository()->toArray();
        $view = View::create()->setData($projects);

        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * get a project
     * [GET] /projects/{id}
     *
     * @param int id
     */
    public function getProjectAction($id)
    {
        $project = $this->getProjectRepository()->find($id);
        if ($project) {
            $view = View::create()->setData($project->toArray());
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Project does not exist.");
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * create a new project
     * [POST] /projects
     *
     * @return void
     */
    public function postProjectsAction()
    {
        // create a new project entity
        $project = new Project();

        // create project form
        $form = $this->createForm(new ProjectType(), $project);

        // get request
        $request = $this->getRequest();

        // decode json
        $data = json_decode($this->getContent(), true);

        // save form and send response
        return $this->get('fos_rest.view_handler')->handle($this->saveForm($form, $data));
    }

    /**
     * modify project
     * [PUT] /projects/{id}
     *
     * @param string $id
     * @return void
     */
    public function putProjectAction($id)
    {
        $project = $this->getProjectRepository()->find($id);

        if ($project) {
            $view = $this->saveForm(
                $this->createForm(new ProjectType(), $project),
                json_decode($this->getRequest()->getContent(), true)<commit_msg>add customer loading from database in post request<commit_after><?php

namespace Dime\TimetrackerBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\View\View;
use Dime\TimetrackerBundle\Entity\Project;
use Dime\TimetrackerBundle\Form\ProjectType;

class ProjectController extends DimeController
{
    /**
     * get project repository
     *
     * @return Dime\TimetrackerBundle\Entity\ProjectRepository
     */
    protected function getProjectRepository()
    {
        return $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Project');
    }

    /**
     * get a list of all projects
     *
     * [GET] /projects
     */
    public function getProjectsAction()
    {
        $projects = $this->getProjectRepository()->toArray();
        $view = View::create()->setData($projects);

        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * get a project
     * [GET] /projects/{id}
     *
     * @param int id
     */
    public function getProjectAction($id)
    {
        $project = $this->getProjectRepository()->find($id);
        if ($project) {
            $view = View::create()->setData($project->toArray());
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Project does not exist.");
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * create a new project
     * [POST] /projects
     *
     * @return void
     */
    public function postProjectsAction()
    {
        // create a new project entity
        $project = new Project();

        // create project form
        $form = $this->createForm(new ProjectType(), $project);

        // get request
        $request = $this->getRequest();

        // decode json
        $data = json_decode($request->getContent(), true);
        
        if (isset($data['customer']) && !empty($data['customer'])) {
            $em = $this->getDoctrine()->getEntityManager();
            $data['customer'] = $em->getRepository('DimeTimetrackerBundle:Customer')->find($data['customer']);
        }
        
        // save form and send response
        return $this->get('fos_rest.view_handler')->handle($this->saveForm($form, $data));
    }

    /**
     * modify project
     * [PUT] /projects/{id}
     *
     * @param string $id
     * @return void
     */
    public function putProjectAction($id)
    {
        $project = $this->getProjectRepository()->find($id);

        if ($project) {
            $view = $this->saveForm(
                $this->createForm(new ProjectType(), $project),
                json_decode($this->getRequest()->getContent(), true)
{'fraction_non_alphanumeric': 0.14778229711408097, 'fraction_numerical': 0.0011621150493898896, 'mean_word_length': 2.3619791666666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '3433638', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1439, 'n_words': 283}