3 namespace Wallabag\CoreBundle\ParamConverter
;
5 use Doctrine\Common\Persistence\ManagerRegistry
;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
7 use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface
;
8 use Symfony\Component\HttpFoundation\Request
;
9 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException
;
10 use Wallabag\UserBundle\Entity\User
;
13 * ParamConverter used in the RSS controller to retrieve the right user according to
14 * username & token given in the url.
16 * @see http://stfalcon.com/en/blog/post/symfony2-custom-paramconverter
18 class UsernameRssTokenConverter
implements ParamConverterInterface
23 * @param ManagerRegistry $registry Manager registry
25 public function __construct(ManagerRegistry
$registry = null)
27 $this->registry
= $registry;
33 * Check, if object supported by our converter
35 public function supports(ParamConverter
$configuration)
37 // If there is no manager, this means that only Doctrine DBAL is configured
38 // In this case we can do nothing and just return
39 if (null === $this->registry
|| !\
count($this->registry
->getManagers())) {
43 // Check, if option class was set in configuration
44 if (null === $configuration->getClass()) {
48 // Get actual entity manager for class
49 $em = $this->registry
->getManagerForClass($configuration->getClass());
51 // Check, if class name is what we need
52 if (null !== $em && 'Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
64 * @throws \InvalidArgumentException When route attributes are missing
65 * @throws NotFoundHttpException When object not found
67 public function apply(Request
$request, ParamConverter
$configuration)
69 $username = $request->attributes
->get('username');
70 $rssToken = $request->attributes
->get('token');
72 if (!$request->attributes
->has('username') || !$request->attributes
->has('token')) {
76 // Get actual entity manager for class
77 $em = $this->registry
->getManagerForClass($configuration->getClass());
79 $userRepository = $em->getRepository($configuration->getClass());
81 // Try to find user by its username and config rss_token
82 $user = $userRepository->findOneByUsernameAndRsstoken($username, $rssToken);
84 if (null === $user || !($user instanceof User
)) {
85 throw new NotFoundHttpException(sprintf('%s not found.', $configuration->getClass()));
88 // Map found user to the route's parameter
89 $request->attributes
->set($configuration->getName(), $user);