Last active
July 8, 2019 14:15
-
-
Save ArchTaqi/5221e670d361ccdd42d63f265bf36c98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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