<commit_before><?php

namespace RestFull\RestFullBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use RestFull\RestFullBundle\Entity\User;
use RestFull\RestFullBundle\Form\UserType;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * User controller.
 *
 * @Route("/")
 */
class UserController extends Controller
{

    /**
     * Lists all User entities.
     *
     * @Route("/user/list", name="user")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $entities = $em->getRepository('RestFullRestFullBundle:User')->findAll();

        return array(
            'entities' => $entities,
        );
    }

    /**
     * return a status 200.
     *
     * @Route("/user/", name="home")
     * @Method("GET")
     */
    public function homeAction()
    {
        $response = new Response('Content', 200, array('content-type' => 'text/html'));
        return $response;
    }

    /**
     * Creates a new User entity.
     *
     * @Route("/user/", name="user_create")
     * @Method("POST")
     * @Template("RestFullRestFullBundle:User: .html.twig")
     */
    public function createAction(Request $request)
    {

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

        //var_dump("<PRE>",$request->request);die;
        $entity = new User();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $repo = $oEm->getRepository("RestFullRestFullBundle:User");
            //$repo->getNumberOfUser();
            //$u = $oEm->getRepository("RestFullRestFullBundle:User")->findAll();
            //$r = count($u);
            //$entity->setId(110);
            //var_dump("<PRE>",$entity);die;

            $em->persist($entity);
            $em->flush($entity);
//            var_dump($entity);die;

            return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to create a User entity.
    *
    * @param User $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(User $entity)
    {
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('user_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new User entity.
     *
     * @Route("/user/new", name="user_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {

        $entity = new User();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
            //'formCustom' => $formCustom->createView(),
        );
    }

    /**
     * Finds and displays a User entity.
     *
     * @Route("/user/{id}/", name="user_show")
     * @Route("/users/{id}/", name="users_show")
     * @Method("GET")
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('RestFullRestFullBundle:User')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }

        $response = new JsonResponse();
        $response->setData(array(
            'id' => $entity->getId(),
            'lastname' => $entity->getLastName(),
            'firstname' => $entity->getfirstName(),
            'email' => $entity->getEmail(),
            'role' => $entity->getRole()
        ));

        return $response;
    }

    /**
     * Displays a form to edit an existing User entity.
     *
     * @Route("/user/{id}/edit", name="user_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('RestFullRestFullBundle:User')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),<commit_msg>{LB} etape 3 + maj<commit_after><?php

namespace RestFull\RestFullBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use RestFull\RestFullBundle\Entity\User;
use RestFull\RestFullBundle\Form\UserType;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * User controller.
 *
 * @Route("/")
 */
class UserController extends Controller
{

    /**
     * Lists all User entities.
     *
     * @Route("/user/list", name="user")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $entities = $em->getRepository('RestFullRestFullBundle:User')->findAll();

        return array(
            'entities' => $entities,
        );
    }

    /**
     * return a status 200.
     *
     * @Route("/user/", name="home")
     * @Method("GET")
     */
    public function homeAction()
    {
        $response = new Response('Content', 200, array('content-type' => 'text/html'));
        return $response;
    }

    /**
     * Creates a new User entity.
     *
     * @Route("/user/", name="user_create")
     * @Method("POST")
     * @Template("RestFullRestFullBundle:User: .html.twig")
     */
    public function createAction(Request $request)
    {

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

        //var_dump("<PRE>",$request->request);die;
        $entity = new User();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $repo = $oEm->getRepository("RestFullRestFullBundle:User");
            //$repo->getNumberOfUser();
            //$u = $oEm->getRepository("RestFullRestFullBundle:User")->findAll();
            //$r = count($u);
            //$entity->setId(110);
            //var_dump("<PRE>",$entity);die;

            $em->persist($entity);
            $em->flush($entity);
//            var_dump($entity);die;

            return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to create a User entity.
    *
    * @param User $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(User $entity)
    {
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('user_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new User entity.
     *
     * @Route("/user/new", name="user_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {

        $entity = new User();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
            //'formCustom' => $formCustom->createView(),
        );
    }

    /**
     * Finds and displays a User entity.
     *
     * @Route("/user/{id}/", name="user_show")
     * @Route("/users/{id}/", name="users_show")
     * @Method("GET")
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('RestFullRestFullBundle:User')->find($id);


        if (!$entity) {

            $array = array(
                'status' => 404,
                'message' => "not found",);
            $response = new Response(json_encode($array), 404);
            $response->headers->set('Content-Type', 'application/json');
            //throw $this->createNotFoundException('Unable to find User entity.');
        }
        else if ($entity->getRole() == "admin") {

            $array = array(
                'status' => 401,
                'message' => "Unauthorized access",);
            $response = new Response(json_encode($array), 401);
            $response->headers->set('Content-Type', 'application/json');

        }
        else {
            $response = new JsonResponse();
            $response->setData(array(
                'id' => $entity->getId(),
                'lastname' => $entity->getLastName(),
                'firstname' => $entity->getfirstName(),
                'email' => $entity->getEmail(),
                'role' => $entity->getRole()
            ));

        }


        return $response;
    }

    /**
     * Displays a form to edit an existing User entity.
     *
     * @Route("/user/{id}/edit", name="user_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('RestFullRestFullBundle:User')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
{'fraction_non_alphanumeric': 0.17224790114140176, 'fraction_numerical': 0.0029242524290161304, 'mean_word_length': 2.184740162210874, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 152, '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': '4937071', 'n_tokens_mistral': 3116, 'n_tokens_neox': 3043, 'n_words': 555}