Commit a9ee2eb0 authored by wilhelmhb's avatar wilhelmhb

Handlers for the entities

parent ea9a89c2
<?php
namespace ApiBundle\Entity\Handler;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormFactoryInterface;
use ApiBundle\Entity\Interfaces\CommonHandlerInterface;
use ApiBundle\Entity\Interfaces\CommonInterface;
use ApiBundle\Form\PsBoxType;
use ApiBundle\Entity\PsBox;
use ApiBundle\Exception\InvalidFormException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
class PsBoxHandler implements CommonHandlerInterface {
private $formFactory;
private $om;
private $entityClass;
private $repository;
public function __construct(ObjectManager $om, $entityClass, FormFactoryInterface $formFactory) {
$this->om = $om;
$this->entityClass = $entityClass;
$this->repository = $this->om->getRepository($this->entityClass);
$this->formFactory = $formFactory;
}
public function get($id) {
return $this->repository->find($id);
}
/**
* Get a list of PsBoxs.
*
* @param int $limit the limit of the result
* @param int $offset starting from the offset
*
* @return array
*/
public function all($limit = 5, $offset = 0)
{
return $this->repository->findBy(array(), null, $limit, $offset);
}
/**
* Create a new PsBox.
*
* @param array $parameters
*
* @return PsBoxInterface
*/
public function post(array $parameters) {
$psbox = $this->createPsBox(); // factory method create an empty PsBox
// Process form does all the magic, validate and hydrate the PsBox Object.
return $this->processForm($psbox, $parameters, 'POST');
}
/**
* Processes the form.
*
* @param PsBoxInterface $psbox
* @param array $parameters
* @param String $method
*
* @return PsBoxInterface
*
* @throws \Acme\BlogBundle\Exception\InvalidFormException
*/
private function processForm(PsBox $psbox, array $parameters, $method = "PUT") {
$parameters = $parameters['apibundle_psbox'];
$form = $this->formFactory->create(new PsBoxType(), $psbox, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method);
if ($form->isValid()) {
$psbox = $form->getData();
$this->om->persist($psbox);
$this->om->flush($psbox);
return $psbox;
}
throw new InvalidFormException('Invalid submitted data', $form);
}
/**
* Presents the form to use to create a new psbox.
*
* @ApiDoc(
* resource = true,
* statusCodes = {
* 200 = "Returned when successful"
* }
* )
*
* @View()
*
* @return FormTypeInterface
*/
public function newAction() {
return $this->createForm(new PsBoxType());
}
private function createPsBox()
{
return new $this->entityClass();
}
public function delete(PsBox $psbox) {
$this->om->remove($psbox);
$this->om->flush();
$response = new JsonResponse(['success' => true], 301);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* Edit a Page, or create if not exist.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function put($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PUT');
}
/**
* Partially update a Page.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function patch($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PATCH');
}
}
<?php
namespace ApiBundle\Entity\Handler;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormFactoryInterface;
use ApiBundle\Entity\Interfaces\CommonHandlerInterface;
use ApiBundle\Entity\Interfaces\CommonInterface;
use ApiBundle\Form\PsClientType;
use ApiBundle\Entity\PsClient;
use ApiBundle\Exception\InvalidFormException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
class PsClientHandler implements CommonHandlerInterface {
private $formFactory;
private $om;
private $entityClass;
private $repository;
public function __construct(ObjectManager $om, $entityClass, FormFactoryInterface $formFactory) {
$this->om = $om;
$this->entityClass = $entityClass;
$this->repository = $this->om->getRepository($this->entityClass);
$this->formFactory = $formFactory;
}
public function get($id) {
return $this->repository->find($id);
}
/**
* Get a list of PsClients.
*
* @param int $limit the limit of the result
* @param int $offset starting from the offset
*
* @return array
*/
public function all($limit = null, $offset = null)
{
return $this->repository->findBy(array(), null, $limit, $offset);
}
/**
* Create a new PsClient.
*
* @param array $parameters
*
* @return PsClientInterface
*/
public function post(array $parameters) {
$psclient = $this->createPsClient(); // factory method create an empty PsClient
// Process form does all the magic, validate and hydrate the PsClient Object.
return $this->processForm($psclient, $parameters, 'POST');
}
/**
* Processes the form.
*
* @param PsClientInterface $psclient
* @param array $parameters
* @param String $method
*
* @return PsClientInterface
*
* @throws \Acme\BlogBundle\Exception\InvalidFormException
*/
private function processForm(PsClient $psclient, array $parameters, $method = "PUT") {
$parameters = $parameters['apibundle_psclient'];
$form = $this->formFactory->create(new PsClientType(), $psclient, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method);
if ($form->isValid()) {
$psclient = $form->getData();
$this->om->persist($psclient);
$this->om->flush($psclient);
return $psclient;
}
throw new InvalidFormException('Invalid submitted data', $form);
}
/**
* Presents the form to use to create a new psclient.
*
* @ApiDoc(
* resource = true,
* statusCodes = {
* 200 = "Returned when successful"
* }
* )
*
* @View()
*
* @return FormTypeInterface
*/
public function newAction() {
return $this->createForm(new PsClientType());
}
private function createPsClient()
{
return new $this->entityClass();
}
public function delete(PsClient $pshive) {
$this->om->remove($pshive);
$this->om->flush();
$response = new JsonResponse(['success' => true], 301);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* Edit a Page, or create if not exist.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function put($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PUT');
}
/**
* Partially update a Page.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function patch($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PATCH');
}
}
<?php
namespace ApiBundle\Entity\Handler;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormFactoryInterface;
use ApiBundle\Entity\Interfaces\CommonHandlerInterface;
use ApiBundle\Entity\Interfaces\CommonInterface;
use ApiBundle\Form\PsCustomNoteCustomerType;
use ApiBundle\Entity\PsCustomNoteCustomer;
use ApiBundle\Exception\InvalidFormException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
class PsCustomNoteCustomerHandler implements CommonHandlerInterface {
private $formFactory;
private $om;
private $entityClass;
private $repository;
public function __construct(ObjectManager $om, $entityClass, FormFactoryInterface $formFactory) {
$this->om = $om;
$this->entityClass = $entityClass;
$this->repository = $this->om->getRepository($this->entityClass);
$this->formFactory = $formFactory;
}
public function get($id) {
return $this->repository->find($id);
}
/**
* Get a list of PsCustomNoteCustomers.
*
* @param int $limit the limit of the result
* @param int $offset starting from the offset
*
* @return array
*/
public function all($limit = null, $offset = null)
{
return $this->repository->findBy(array(), null, $limit, $offset);
}
/**
* Create a new PsCustomNoteCustomer.
*
* @param array $parameters
*
* @return PsCustomNoteCustomerInterface
*/
public function post(array $parameters) {
$pscustomnotecustomer = $this->createPsCustomNoteCustomer(); // factory method create an empty PsCustomNoteCustomer
// Process form does all the magic, validate and hydrate the PsCustomNoteCustomer Object.
return $this->processForm($pscustomnotecustomer, $parameters, 'POST');
}
/**
* Processes the form.
*
* @param PsCustomNoteCustomerInterface $pscustomnotecustomer
* @param array $parameters
* @param String $method
*
* @return PsCustomNoteCustomerInterface
*
* @throws \Acme\BlogBundle\Exception\InvalidFormException
*/
private function processForm(PsCustomNoteCustomer $pscustomnotecustomer, array $parameters, $method = "PUT") {
$parameters = $parameters['apibundle_pscustomnotecustomer'];
$form = $this->formFactory->create(new PsCustomNoteCustomerType(), $pscustomnotecustomer, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method);
if ($form->isValid()) {
$pscustomnotecustomer = $form->getData();
$this->om->persist($pscustomnotecustomer);
$this->om->flush($pscustomnotecustomer);
return $pscustomnotecustomer;
}
throw new InvalidFormException('Invalid submitted data', $form);
}
/**
* Presents the form to use to create a new pscustomnotecustomer.
*
* @ApiDoc(
* resource = true,
* statusCodes = {
* 200 = "Returned when successful"
* }
* )
*
* @View()
*
* @return FormTypeInterface
*/
public function newAction() {
return $this->createForm(new PsCustomNoteCustomerType());
}
private function createPsCustomNoteCustomer()
{
return new $this->entityClass();
}
public function delete(PsCustomNoteCustomer $pscustomnotecustomer) {
$this->om->remove($pscustomnotecustomer);
$this->om->flush();
$response = new JsonResponse(['success' => true], 301);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* Edit a Page, or create if not exist.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function put($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PUT');
}
/**
* Partially update a Page.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function patch($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PATCH');
}
}
<?php
namespace ApiBundle\Entity\Handler;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormFactoryInterface;
use ApiBundle\Entity\Interfaces\CommonHandlerInterface;
use ApiBundle\Entity\Interfaces\CommonInterface;
use ApiBundle\Form\PsCustomNoteType;
use ApiBundle\Entity\PsCustomNote;
use ApiBundle\Exception\InvalidFormException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
class PsCustomNoteHandler implements CommonHandlerInterface {
private $formFactory;
private $om;
private $entityClass;
private $repository;
public function __construct(ObjectManager $om, $entityClass, FormFactoryInterface $formFactory) {
$this->om = $om;
$this->entityClass = $entityClass;
$this->repository = $this->om->getRepository($this->entityClass);
$this->formFactory = $formFactory;
}
public function get($id) {
return $this->repository->find($id);
}
/**
* Get a list of PsCustomNotes.
*
* @param int $limit the limit of the result
* @param int $offset starting from the offset
*
* @return array
*/
public function all($limit = null, $offset = null)
{
return $this->repository->findBy(array(), null, $limit, $offset);
}
/**
* Create a new PsCustomNote.
*
* @param array $parameters
*
* @return PsCustomNoteInterface
*/
public function post(array $parameters) {
$pscustomnote = $this->createPsCustomNote(); // factory method create an empty PsCustomNote
// Process form does all the magic, validate and hydrate the PsCustomNote Object.
return $this->processForm($pscustomnote, $parameters, 'POST');
}
/**
* Processes the form.
*
* @param PsCustomNoteInterface $pscustomnote
* @param array $parameters
* @param String $method
*
* @return PsCustomNoteInterface
*
* @throws \Acme\BlogBundle\Exception\InvalidFormException
*/
private function processForm(PsCustomNote $pscustomnote, array $parameters, $method = "PUT") {
$parameters = $parameters['apibundle_pscustomnote'];
$form = $this->formFactory->create(new PsCustomNoteType(), $pscustomnote, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method);
if ($form->isValid()) {
$pscustomnote = $form->getData();
$this->om->persist($pscustomnote);
$this->om->flush($pscustomnote);
return $pscustomnote;
}
throw new InvalidFormException('Invalid submitted data', $form);
}
/**
* Presents the form to use to create a new pscustomnote.
*
* @ApiDoc(
* resource = true,
* statusCodes = {
* 200 = "Returned when successful"
* }
* )
*
* @View()
*
* @return FormTypeInterface
*/
public function newAction() {
return $this->createForm(new PsCustomNoteType());
}
private function createPsCustomNote()
{
return new $this->entityClass();
}
public function delete(PsCustomNote $pscustomnote) {
$this->om->remove($pscustomnote);
$this->om->flush();
$response = new JsonResponse(['success' => true], 301);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* Edit a Page, or create if not exist.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function put($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PUT');
}
/**
* Partially update a Page.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function patch($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PATCH');
}
}
<?php
namespace ApiBundle\Entity\Handler;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormFactoryInterface;
use ApiBundle\Entity\Interfaces\CommonHandlerInterface;
use ApiBundle\Entity\Interfaces\CommonInterface;
use ApiBundle\Entity\PsCustomer;
use ApiBundle\Form\PsCustomerType;
use ApiBundle\Exception\InvalidFormException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Debug\Exception\ClassNotFoundException;
class PsCustomerHandler implements CommonHandlerInterface {
private $formFactory;
private $om;
private $entityClass;
private $repository;
public function __construct(ObjectManager $om, $entityClass, FormFactoryInterface $formFactory) {
$this->om = $om;
$this->entityClass = $entityClass;
$this->repository = $this->om->getRepository($this->entityClass);
$this->formFactory = $formFactory;
}
public function get($id) {
return $this->repository->find($id);
}
/**
* Get a list of PsCustomers.
*
* @param int $limit the limit of the result
* @param int $offset starting from the offset
*
* @return array
*/
public function all($limit = 5, $offset = 0)
{
return $this->repository->findBy(array(), null, $limit, $offset);
}
/**
* Create a new PsCustomer.
*
* @param array $parameters
*
* @return PsCustomerInterface
*/
public function post(array $parameters) {
$pscustomer = $this->createPsCustomer(); // factory method create an empty PsCustomer
// Process form does all the magic, validate and hydrate the PsCustomer Object.
return $this->processForm($pscustomer, $parameters, 'POST');
}
/**
* Processes the form.
*
* @param PsCustomerInterface $pscustomer
* @param array $parameters
* @param String $method
*
* @return PsCustomerInterface
*
* @throws \Acme\BlogBundle\Exception\InvalidFormException
*/
private function processForm(PsCustomer $pscustomer, array $parameters, $method = "PUT") {
$parameters = $parameters['apibundle_pscustomer'];
$form = $this->formFactory->create(new PsCustomerType(), $pscustomer, array('method' => $method));
$form->submit($parameters, 'PATCH' !== $method);
if ($form->isValid()) {
$pscustomer = $form->getData();
if((isset($parameters['active']))&&($parameters['active'] == 0)) {
$pscustomer->setActive(false);
}
$this->om->persist($pscustomer);
$this->om->flush($pscustomer);
return $pscustomer;
}
throw new InvalidFormException('Invalid submitted data', $form);
}
/**
* Presents the form to use to create a new pscustomer.
*
* @ApiDoc(
* resource = true,
* statusCodes = {
* 200 = "Returned when successful"
* }
* )
*
* @View()
*
* @return FormTypeInterface
*/
public function newAction() {
return $this->createForm(new PsCustomerType());
}
private function createPsCustomer()
{
return new $this->entityClass();
}
public function delete(PsCustomer $pshive) {
$this->om->remove($pshive);
$this->om->flush();
$response = new JsonResponse(['success' => true], 301);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* Edit a Page, or create if not exist.
*
* @param PageInterface $page
* @param array $parameters
*
* @return PageInterface
*/
public function put($page, array $parameters)
{
return $this->processForm($page, $parameters, 'PUT');
}
/**
* Partially update a Page.
*
* @param PageInterface $page
* @param array $parameters
*