<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Suscriber;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;

/**
 * Suscriber controller.
 *
 * @Route("suscriber")
 */
class SuscriberController extends Controller
{
    /**
     * Lists all suscriber entities.
     *
     * @Route("/", name="suscriber_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $suscribers = $em->getRepository('AppBundle:Suscriber')->findAll();

        return $this->render('suscriber/index.html.twig', array(
            'suscribers' => $suscribers,
        ));
    }

    /**
     * Creates a new suscriber entity.
     *
     * @Route("/new", name="suscriber_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $suscriber = new Suscriber();
        $form = $this->createForm('AppBundle\Form\SuscriberType', $suscriber);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($suscriber);
            $em->flush();

            return $this->redirectToRoute('suscriber_show', array('id' => $suscriber->getId()));
        }

        return $this->render('suscriber/new.html.twig', array(
            'suscriber' => $suscriber,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a suscriber entity.
     *
     * @Route("/{id}", name="suscriber_show")
     * @Method("GET")
     */
    public function showAction(Suscriber $suscriber)
    {
        $deleteForm = $this->createDeleteForm($suscriber);

        return $this->render('suscriber/show.html.twig', array(
            'suscriber' => $suscriber,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing suscriber entity.
     *
     * @Route("/{id}/edit", name="suscriber_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Suscriber $suscriber)
    {
        $deleteForm = $this->createDeleteForm($suscriber);
        $editForm = $this->createForm('AppBundle\Form\SuscriberType', $suscriber);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('suscriber_edit', array('id' => $suscriber->getId()));
        }

        return $this->render('suscriber/edit.html.twig', array(
            'suscriber' => $suscriber,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a suscriber entity.
     *
     * @Route("/{id}", name="suscriber_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Suscriber $suscriber)
    {
        $form = $this->createDeleteForm($suscriber);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($suscriber);
            $em->flush();
        }

        return $this->redirectToRoute('suscriber_index');
    }

    /**
     * Creates a form to delete a suscriber entity.
     *
     * @param Suscriber $suscriber The suscriber entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Suscriber $suscriber)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('suscriber_delete', array('id' => $suscriber->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}
