]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverter.php
Added URL compatibility
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / ParamConverter / UsernameRssTokenConverter.php
1 <?php
2
3 namespace Wallabag\CoreBundle\ParamConverter;
4
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;
11
12 /**
13 * ParamConverter used in the RSS controller to retrieve the right user according to
14 * username & token given in the url.
15 *
16 * @see http://stfalcon.com/en/blog/post/symfony2-custom-paramconverter
17 */
18 class UsernameRssTokenConverter implements ParamConverterInterface
19 {
20 private $registry;
21
22 /**
23 * @param ManagerRegistry $registry Manager registry
24 */
25 public function __construct(ManagerRegistry $registry = null)
26 {
27 $this->registry = $registry;
28 }
29
30 /**
31 * {@inheritdoc}
32 *
33 * Check, if object supported by our converter
34 */
35 public function supports(ParamConverter $configuration)
36 {
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())) {
40 return false;
41 }
42
43 // Check, if option class was set in configuration
44 if (null === $configuration->getClass()) {
45 return false;
46 }
47
48 // Get actual entity manager for class
49 $em = $this->registry->getManagerForClass($configuration->getClass());
50
51 // Check, if class name is what we need
52 if (null !== $em && 'Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
53 return false;
54 }
55
56 return true;
57 }
58
59 /**
60 * {@inheritdoc}
61 *
62 * Applies converting
63 *
64 * @throws \InvalidArgumentException When route attributes are missing
65 * @throws NotFoundHttpException When object not found
66 */
67 public function apply(Request $request, ParamConverter $configuration)
68 {
69 $username = $request->attributes->get('username');
70 $rssToken = $request->attributes->get('token');
71
72 if (!$request->attributes->has('username') || !$request->attributes->has('token')) {
73 return false;
74 }
75
76 // Get actual entity manager for class
77 $em = $this->registry->getManagerForClass($configuration->getClass());
78
79 $userRepository = $em->getRepository($configuration->getClass());
80
81 // Try to find user by its username and config rss_token
82 $user = $userRepository->findOneByUsernameAndRsstoken($username, $rssToken);
83
84 if (null === $user || !($user instanceof User)) {
85 throw new NotFoundHttpException(sprintf('%s not found.', $configuration->getClass()));
86 }
87
88 // Map found user to the route's parameter
89 $request->attributes->set($configuration->getName(), $user);
90 }
91 }