<commit_before><?php

namespace Dime\TimetrackerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\View\View;

class ActivitiesController extends Controller
{
    /**
     * [GET] /activities
     *
     * @Route("/")
     */
    public function getActivitiesAction()
    {
        $activities = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->allToArray();

        $view = View::create()
                  ->setStatusCode(200)
                  ->setData($activities)
                  ;

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

    /**
     * load activity
     *
     * [GET] /activity/{id}
     */
    public function getActivityAction($id)
    {
        $activity = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->find($id);
        if ($activity) {
            $view = View::create()->setStatusCode(200);
            $view->setData(array(
                'id'    => $activity->getId(),
                'name'  => $activity->getName(),
                'alias' => $activity->getAlias()
            ));
        } else {
            $view = View::create()->setStatusCode(404);
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * create activity
     * [POST] /activity
     * 
     * @return void
     */
    public function postActivityAction()
    {
        $activity = new Activity();
        $form = $this->getForm($activity);
        $form->bindRequest($this->getRequest());
        $this->persist($form, $activity);
        $view->setData($form->getData());
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * modify activity
     * [PUT] /activity/{id}
     * 
     * @param string $id
     * @return void
     */
    public function putActivityAction($id)
    {
        $activity = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->find($id);
        $form = $this->getForm($activity);
        $this->persist($form, $activity);
        $view->setData($form->getData());
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * delete activity
     * [DELETE] /activity/{id}
     *
     * @param int $id
     * @return void
     */
    public function deleteActivityAction($id)
    {
        if ($activity = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->find($id)) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->remove($activity);
        }
    }

    /**
     * persist activity
     * 
     * @param $form
     * @param Dime\TimetrackerBundle\Entity\Activity $activity
     */
    protected function persist($form, Dime\TimetrackerBundle\Entity\Activity $activity)
    {
        $form->bindRequest($this->getRequest());
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($activity);
            $em->flush();
        }
    }

    protected function getForm($activity)
    {
        return $this->formFactory->createBuilder('form', $activity)
            ->add('name',  'string', array('required' => true))
            ->add('alias', 'string', array('required' => true))
            ->getForm();
    }<commit_msg>fixed activity api getter and form (where we should work on ...)<commit_after><?php

namespace Dime\TimetrackerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\RestBundle\View\View;

class ActivitiesController extends Controller
{
    /**
     * [GET] /activities
     *
     * @Route("/")
     */
    public function getActivitiesAction()
    {
        $activities = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->allToArray();

        $view = View::create()
                  ->setStatusCode(200)
                  ->setData($activities)
                  ;

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

    /**
     * load activity
     *
     * [GET] /activity/{id}
     */
    public function getActivityAction($id)
    {
        $activity = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->find($id);
        if ($activity) {
            $view = View::create()->setStatusCode(200);
            $view->setData($activity->toArray());
        } else {
            $view = View::create()->setStatusCode(404);
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * create activity
     * [POST] /activity
     * 
     * @return void
     */
    public function postActivityAction()
    {
        $activity = new Activity();
        $form = $this->getForm($activity);
        $form->bindRequest($this->getRequest());
        $this->persist($form, $activity);
        $view->setData($form->getData());
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * modify activity
     * [PUT] /activity/{id}
     * 
     * @param string $id
     * @return void
     */
    public function putActivityAction($id)
    {
        $activity = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->find($id);
        $form = $this->getForm($activity);
        $this->persist($form, $activity);
        $view->setData($form->getData());
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * delete activity
     * [DELETE] /activity/{id}
     *
     * @param int $id
     * @return void
     */
    public function deleteActivityAction($id)
    {
        if ($activity = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Activity')->find($id)) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->remove($activity);
        }
    }

    /**
     * persist activity
     * 
     * @param $form
     * @param Dime\TimetrackerBundle\Entity\Activity $activity
     */
    protected function persist($form, Dime\TimetrackerBundle\Entity\Activity $activity)
    {
        $form->bindRequest($this->getRequest());
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($activity);
            $em->flush();
        }
    }

    /**
     * getForm 
     * 
     * @todo
     *
     * @param mixed $activity 
     * @return void
     */
    protected function getForm($activity)
    {
        return $this->formFactory->createBuilder('form', $activity)
            ->add('name',          'string',   array('required' => true))
            ->add('duration',      'integer',  array('required' => false))
            ->add('startedAt',     'datetime', array('required' => false))
            ->add('stoppedAt',     'datetime', array('required' => false))
            ->add('description',   'string',   array('required' => false))
            ->add('rate',          'integer',  array('required' => false))
            ->add('service',       'Service',  array('required' => false))
            ->add('customer',      'Customer', array('required' => false))
            ->add('project',       'Project',  array('required' => false))
            ->getForm();
    }
{'fraction_non_alphanumeric': 0.16924551554155826, 'fraction_numerical': 0.0024647405175955085, 'mean_word_length': 2.2247240618101545, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 128, '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': '7486492', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2069, 'n_words': 330}