Skip to content

Instantly share code, notes, and snippets.

@khepin
Created June 20, 2012 11:36
Show Gist options
  • Save khepin/2959468 to your computer and use it in GitHub Desktop.
Save khepin/2959468 to your computer and use it in GitHub Desktop.
<?php
namespace Khepin\UserValidatorBundle\Aop;
use CG\Proxy\MethodInterceptorInterface;
use CG\Proxy\MethodInvocation;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Validator\Validator;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
class Interceptor implements MethodInterceptorInterface
{
public function __construct(...)
{
// Set all class properties
}
/**
* Redirects the user to his profile page if his profile was found to not be complete yet.
*
* @param MethodInvocation $invocation
* @return mixed|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function intercept(MethodInvocation $invocation)
{
if(!$this->context->isGranted('ROLE_USER')){
return $invocation->proceed();
}
$user = $this->context->getToken()->getUser();
$annotation = $this->reader->getMethodAnnotation($invocation->reflection, self::$annotation_class);
$errors = $this->validator->validate($user, $annotation->getValidationGroup());
if(count($errors) > 0) {
$this->session->getFlashBag()->add(
'error',
'We need some more info first.'
);
foreach($errors as $error){
$this->session->getFlashBag()->add(
'warning',
$error->getPropertyPath() . ': ' . $error->getMessage()
);
}
$redirect = $this->router->generate($annotation->getRoute());
return new RedirectResponse($redirect);
}
return $invocation->proceed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment