]>
Commit | Line | Data |
---|---|---|
0c83fd59 J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\ParamConverter; | |
4 | ||
0c83fd59 J |
5 | use Doctrine\Common\Persistence\ManagerRegistry; |
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
619cc453 | 7 | use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
0c83fd59 J |
8 | use Symfony\Component\HttpFoundation\Request; |
9 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
1210dae1 | 10 | use 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 | */ | |
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 | |
1210dae1 | 52 | if ('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 J |
71 | |
72 | // Check, if route attributes exists | |
73 | if (null === $username || null === $rssToken) { | |
74 | throw new \InvalidArgumentException('Route attribute is missing'); | |
75 | } | |
76 | ||
77 | // Get actual entity manager for class | |
78 | $em = $this->registry->getManagerForClass($configuration->getClass()); | |
79 | ||
80 | $userRepository = $em->getRepository($configuration->getClass()); | |
81 | ||
82 | // Try to find user by its username and config rss_token | |
83 | $user = $userRepository->findOneByUsernameAndRsstoken($username, $rssToken); | |
84 | ||
85 | if (null === $user || !($user instanceof User)) { | |
86 | throw new NotFoundHttpException(sprintf('%s not found.', $configuration->getClass())); | |
87 | } | |
88 | ||
89 | // Map found user to the route's parameter | |
90 | $request->attributes->set($configuration->getName(), $user); | |
91 | } | |
92 | } |