]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/DeveloperController.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / DeveloperController.php
1 <?php
2
3 namespace Wallabag\ApiBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 use Symfony\Component\HttpFoundation\Request;
8 use Wallabag\ApiBundle\Entity\Client;
9 use Wallabag\ApiBundle\Form\Type\ClientType;
10
11 class DeveloperController extends Controller
12 {
13 /**
14 * List all clients and link to create a new one.
15 *
16 * @Route("/developer", name="developer")
17 *
18 * @return \Symfony\Component\HttpFoundation\Response
19 */
20 public function indexAction()
21 {
22 $clients = $this->getDoctrine()->getRepository('WallabagApiBundle:Client')->findByUser($this->getUser()->getId());
23
24 return $this->render('@WallabagCore/themes/common/Developer/index.html.twig', [
25 'clients' => $clients,
26 ]);
27 }
28
29 /**
30 * Create a client (an app).
31 *
32 * @param Request $request
33 *
34 * @Route("/developer/client/create", name="developer_create_client")
35 *
36 * @return \Symfony\Component\HttpFoundation\Response
37 */
38 public function createClientAction(Request $request)
39 {
40 $em = $this->getDoctrine()->getManager();
41 $client = new Client($this->getUser());
42 $clientForm = $this->createForm(ClientType::class, $client);
43 $clientForm->handleRequest($request);
44
45 if ($clientForm->isSubmitted() && $clientForm->isValid()) {
46 $client->setAllowedGrantTypes(['client_credentials', 'token', 'authorization_code', 'password', 'refresh_token']);
47 $em->persist($client);
48 $em->flush();
49
50 $this->get('session')->getFlashBag()->add(
51 'notice',
52 $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
53 );
54
55 return $this->render('@WallabagCore/themes/common/Developer/client_parameters.html.twig', [
56 'client_id' => $client->getPublicId(),
57 'client_secret' => $client->getSecret(),
58 'client_name' => $client->getName(),
59 ]);
60 }
61
62 return $this->render('@WallabagCore/themes/common/Developer/client.html.twig', [
63 'form' => $clientForm->createView(),
64 ]);
65 }
66
67 /**
68 * Remove a client.
69 *
70 * @param Client $client
71 *
72 * @Route("/developer/client/delete/{id}", requirements={"id" = "\d+"}, name="developer_delete_client")
73 *
74 * @return \Symfony\Component\HttpFoundation\RedirectResponse
75 */
76 public function deleteClientAction(Client $client)
77 {
78 if (null === $this->getUser() || $client->getUser()->getId() !== $this->getUser()->getId()) {
79 throw $this->createAccessDeniedException('You can not access this client.');
80 }
81
82 $em = $this->getDoctrine()->getManager();
83 $em->remove($client);
84 $em->flush();
85
86 $this->get('session')->getFlashBag()->add(
87 'notice',
88 $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()])
89 );
90
91 return $this->redirect($this->generateUrl('developer'));
92 }
93
94 /**
95 * Display developer how to use an existing app.
96 *
97 * @Route("/developer/howto/first-app", name="developer_howto_firstapp")
98 *
99 * @return \Symfony\Component\HttpFoundation\Response
100 */
101 public function howtoFirstAppAction()
102 {
103 return $this->render('@WallabagCore/themes/common/Developer/howto_app.html.twig');
104 }
105 }