aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverter.php
blob: 4a2fcab5ffc9428b35d7fd8a0d55188d29a00007 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace Wallabag\CoreBundle\ParamConverter;

use Doctrine\Common\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Wallabag\UserBundle\Entity\User;

/**
 * ParamConverter used in the RSS controller to retrieve the right user according to
 * username & token given in the url.
 *
 * @see http://stfalcon.com/en/blog/post/symfony2-custom-paramconverter
 */
class UsernameRssTokenConverter implements ParamConverterInterface
{
    private $registry;

    /**
     * @param ManagerRegistry $registry Manager registry
     */
    public function __construct(ManagerRegistry $registry = null)
    {
        $this->registry = $registry;
    }

    /**
     * {@inheritdoc}
     *
     * Check, if object supported by our converter
     */
    public function supports(ParamConverter $configuration)
    {
        // If there is no manager, this means that only Doctrine DBAL is configured
        // In this case we can do nothing and just return
        if (null === $this->registry || !\count($this->registry->getManagers())) {
            return false;
        }

        // Check, if option class was set in configuration
        if (null === $configuration->getClass()) {
            return false;
        }

        // Get actual entity manager for class
        $em = $this->registry->getManagerForClass($configuration->getClass());

        // Check, if class name is what we need
        if (null !== $em && 'Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
            return false;
        }

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * Applies converting
     *
     * @throws \InvalidArgumentException When route attributes are missing
     * @throws NotFoundHttpException     When object not found
     */
    public function apply(Request $request, ParamConverter $configuration)
    {
        $username = $request->attributes->get('username');
        $rssToken = $request->attributes->get('token');

        if (!$request->attributes->has('username') || !$request->attributes->has('token')) {
            return false;
        }

        // Get actual entity manager for class
        $em = $this->registry->getManagerForClass($configuration->getClass());

        $userRepository = $em->getRepository($configuration->getClass());

        // Try to find user by its username and config rss_token
        $user = $userRepository->findOneByUsernameAndRsstoken($username, $rssToken);

        if (null === $user || !($user instanceof User)) {
            throw new NotFoundHttpException(sprintf('%s not found.', $configuration->getClass()));
        }

        // Map found user to the route's parameter
        $request->attributes->set($configuration->getName(), $user);
    }
}