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