<commit_before><?php

namespace Dime\TimetrackerBundle\Controller;

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

class ServicesController extends DimeController
{
    /**
     * [GET] /services
     *
     * @Route("/")
     */
    public function getServicesAction()
    {
        $services = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Service')->allToArray();
        $view = View::create()->setStatusCode(200);
        $view->setData($services);

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

    /**
     * load service
     * [GET] /services/{id}
     *
     * @param int $id
     */
    public function getServiceAction($id)
    {
        $service = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Service')->find($id);
        if ($service) {
            $view = View::create()->setStatusCode(200);
            $view->setData($service->toArray());
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Service does not exist.");
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * create service
     * [POST] /services
     *
     * @return void
     */
    public function postServicesAction()
    {
        // create new service
        $service = new Service();
        
        // create service form
        $form = $this->createForm(new ServiceType(), $service);
        
        // get request
        $request = $this->getRequest();
    
        // convert json to assoc array
        $data = json_decode($request->getContent(), true);

        return $this->get('fos_rest.view_handler')->handle($this->saveForm($form, $data));
    }

    /**
     * modify service
     * [PUT] /services/{id}
     *
     * @param int $id
     * @return void
     */
    public function putServicesAction($id)
    {
        $service = $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Service')->find($id);
                
        if ($service) {
            $view = $this->saveForm(
                $this->createForm(new ServiceType(), $service),
                json_decode($this->getRequest()->getContent(), true)
            );
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Service does not exists.");
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * delete service
     * [DELETE] /services/{id}
     *
     * @param int $id
     * @return void
     */
    public function deleteServicesAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $service = $em->getRepository('DimeTimetrackerBundle:Service')->find($id);
        
        if ($service) {
            $em->remove($service);
            $em->flush();
            
            $view = View::create()->setStatusCode(200);
            $view->setData("Service has been removed.");
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Service does not exists.");
        }<commit_msg>intruduce getter for service repository in ServicesController; do not set status 200 explicitly, because this is the default status code<commit_after><?php

namespace Dime\TimetrackerBundle\Controller;

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

class ServicesController extends DimeController
{
    /**
     * get service repository 
     * 
     * @return Dime\TimetrackerBundle\Entity\ServiceRepository
     */
    protected function getServiceRepository()
    {
        return $this->getDoctrine()->getRepository('DimeTimetrackerBundle:Service');
    }

    /**
     * [GET] /services
     *
     * @Route("/")
     */
    public function getServicesAction()
    {
        $services = $this->getServiceRepository()->allToArray();
        $view = View::create()->setData($services);

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

    /**
     * load service
     * [GET] /services/{id}
     *
     * @param int $id
     */
    public function getServiceAction($id)
    {
        $service = $this->getServiceRepository()->find($id);
        if ($service) {
            $view = View::create()->setData($service->toArray());
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Service does not exist.");
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * create service
     * [POST] /services
     *
     * @return void
     */
    public function postServicesAction()
    {
        // create new service
        $service = new Service();
        
        // create service form
        $form = $this->createForm(new ServiceType(), $service);
        
        // get request
        $request = $this->getRequest();
    
        // convert json to assoc array
        $data = json_decode($request->getContent(), true);

        return $this->get('fos_rest.view_handler')->handle($this->saveForm($form, $data));
    }

    /**
     * modify service
     * [PUT] /services/{id}
     *
     * @param int $id
     * @return void
     */
    public function putServicesAction($id)
    {
        $service = $this->getServiceRepository()->find($id);
                
        if ($service) {
            $view = $this->saveForm(
                $this->createForm(new ServiceType(), $service),
                json_decode($this->getRequest()->getContent(), true)
            );
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Service does not exists.");
        }
        return $this->get('fos_rest.view_handler')->handle($view);
    }

    /**
     * delete service
     * [DELETE] /services/{id}
     *
     * @param int $id
     * @return void
     */
    public function deleteServicesAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $service = $em->getRepository('DimeTimetrackerBundle:Service')->find($id);
        
        if ($service) {
            $em->remove($service);
            $em->flush();
            
            $view = View::create()->setData("Service has been removed.");
        } else {
            $view = View::create()->setStatusCode(404);
            $view->setData("Service does not exists.");
        }
{'fraction_non_alphanumeric': 0.15265123226288274, 'fraction_numerical': 0.004480955937266617, 'mean_word_length': 2.176470588235294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, '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': '2012789', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1867, 'n_words': 342}