]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverter.php
php-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / ParamConverter / UsernameRssTokenConverter.php
CommitLineData
0c83fd59
J
1<?php
2
3namespace Wallabag\CoreBundle\ParamConverter;
4
0c83fd59
J
5use Doctrine\Common\Persistence\ManagerRegistry;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
619cc453 7use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
0c83fd59
J
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1210dae1 10use Wallabag\UserBundle\Entity\User;
0c83fd59
J
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 */
18class 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
2a1ceb67 39 if (null === $this->registry || !\count($this->registry->getManagers())) {
0c83fd59
J
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
40e21962 52 if (null !== $em && 'Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
0c83fd59
J
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');
8ce32af6 70 $rssToken = $request->attributes->get('token');
0c83fd59 71
40e21962
JB
72 if (!$request->attributes->has('username') || !$request->attributes->has('token')) {
73 return false;
0c83fd59
J
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}