Skip to content

Instantly share code, notes, and snippets.

@kix
Last active August 29, 2015 14:16
Show Gist options
  • Save kix/743f28b58453e3a161f6 to your computer and use it in GitHub Desktop.
Save kix/743f28b58453e3a161f6 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: kix
* Date: 24/02/15
* Time: 19:34
*/
namespace AppBundle\Controller;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use FOS\RestBundle\Controller\FOSRestController;
use Knp\Component\Pager\PaginatorInterface;
use FOS\RestBundle\Controller\Annotations\View;
class AbstractRestController extends FOSRestController
{
const PER_PAGE = 10;
/**
* @var \Symfony\Component\Form\FormFactory
*/
private $formFactory;
/**
* @var ObjectManager
*/
private $em;
/**
* @var EntityRepository
*/
private $repository;
/**
* @var \Knp\Component\Pager\PaginatorInterface
*/
private $paginator;
/**
* @var string
*/
private $formTypeClass;
/**
* @param FormFactoryInterface $formFactory
* @param ObjectManager $em
* @param PaginatorInterface $paginator
*/
public function __construct(FormFactoryInterface $formFactory, ObjectManager $em, PaginatorInterface $paginator)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->paginator = $paginator;
}
/**
* @param EntityRepository $repository
*/
public function setRepository($repository)
{
$this->repository = $repository;
}
/**
* @param $formTypeClass string
*/
public function setFormType($formTypeClass)
{
$this->formTypeClass = $formTypeClass;
}
/**
* @View(serializerGroups={"list"})
*
* @param Request $request
* @return mixed
*/
public function cgetAction(Request $request)
{
$qb = $this->repository->createQueryBuilder('c');
$pagination = $this->paginator->paginate(
$qb,
$request->query->get('page', 1),
self::PER_PAGE
);
return $pagination->getItems();
}
/**
* @View(serializerGroups={"show"})
*
* @param $id
* @return null|object
*/
public function getAction($id)
{
return $this->repository->find($id);
}
/**
* @View(serializerGroups={"show"})
*
* @param Request $request
* @return mixed|\Symfony\Component\Form\FormInterface
*/
public function postAction(Request $request)
{
$form = $this->formFactory->create(new $this->formTypeClass());
$form->submit($request);
if ($form->isValid()) {
$object = $form->getData();
$this->em->persist($object);
$this->em->flush();
return $object;
}
return $form;
}
public function putAction($id, Request $request)
{
$category = $this->getAction($id);
$form = $this->formFactory->create(new $this->formTypeClass(), $category);
$form->submit($request);
if ($form->isValid()) {
$object = $form->getData();
$this->em->persist($object);
$this->em->flush();
return $object;
}
return $form;
}
}
services:
app.category_repository:
class: Doctrine\ORM\EntityRepository
factory_service: doctrine
factory_method: getRepository
arguments:
- %wld_app.category.class%
app.abstract_controller:
class: %app.abstract_controller.class%
abstract: true
arguments:
- @form.factory
- @doctrine.orm.entity_manager
- @knp_paginator
app.category_controller:
class: %app.category_controller.class%
parent: app.abstract_controller
calls:
- [ setFormType, [ %app.category_type.class% ] ]
- [ setRepository, [ @app.category_repository ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment