Skip to content

Instantly share code, notes, and snippets.

@ArchTaqi
Last active July 8, 2019 14:15
Show Gist options
  • Save ArchTaqi/5221e670d361ccdd42d63f265bf36c98 to your computer and use it in GitHub Desktop.
Save ArchTaqi/5221e670d361ccdd42d63f265bf36c98 to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Event\RepositoryEvent;
/**
* Class OurAbstractController
* @package App\Controller
*/
abstract class OurAbstractController extends AbstractController
{
/**
* Standard logic for creating resource
* @param Request $request
* @param object $entity
* @param array $validationGroups
* @return $entity | ConstraintViolationListInterface
*/
public function createResource(Request $request, $entity, array $validationGroups = [])
{
$event = new RepositoryEvent($entity, 'create');
$this->get('event_dispatcher')->dispatch('okApp.pre_validate', $event);
$errors = $this->validate($entity, $validationGroups);
if (count($errors) > 0) {
return $errors;
}
$this->getDoctrine()->getManager()->persist($entity);
$this->get('event_dispatcher')->dispatch('okApp.pre_flush', $event);
$this->getDoctrine()->getManager()->flush(); // save changes to already attached entity
$this->get('event_dispatcher')->dispatch('okApp.post_flush', $event);
return $entity;
}
/**
* Standard logic for updating resource
*
* @param Request $request
* @param $entity
* @param array $validationGroups
* @return mixed
*/
public function updateResource(Request $request, $entity, array $validationGroups = [])
{
$event = new RepositoryEvent($entity, 'update');
$this->get('event_dispatcher')->dispatch('okApp.pre_validate', $event);
$errors = $this->validate($entity, $validationGroups);
if (count($errors) > 0) {
return $errors;
}
$this->getDoctrine()->getManager()->persist($entity);
$this->get('event_dispatcher')->dispatch('okApp.pre_flush', $event);
$this->getDoctrine()->getManager()->flush();
$this->get('event_dispatcher')->dispatch('okApp.post_flush', $event);
return $entity;
}
/**
* Standard logic for deletion resource
*
* @param $entity
* @return mixed
*/
public function deleteResource($entity)
{
$this->getDoctrine()->getManager()->remove($entity);
$event = new RepositoryEvent($entity, 'delete');
$this->get('event_dispatcher')->dispatch('okApp.pre_validate', $event);
$this->get('event_dispatcher')->dispatch('okApp.pre_flush', $event);
$this->getDoctrine()->getManager()->flush();
$this->get('event_dispatcher')->dispatch('okApp.post_flush', $event);
return $entity;
}
/**
* @param $entity
* @param array $groups
* @return ConstraintViolationListInterface
*/
protected function validate($entity, $groups = null)
{
return $this->get('validator')->validate($entity, empty($groups) ? null : $groups);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment