]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/UserRestController.php
Create a client when creating a user using the api
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / UserRestController.php
index 8f675b8df9431c93f64ef23fc45319c3f23e5f19..becbbb9ef4c0d77f5f024fe06cef1ba81e885f47 100644 (file)
@@ -9,6 +9,7 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Wallabag\UserBundle\Entity\User;
+use Wallabag\ApiBundle\Entity\Client;
 
 class UserRestController extends WallabagRestController
 {
@@ -97,29 +98,38 @@ class UserRestController extends WallabagRestController
                 ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
         }
 
+        // create a default client
+        $client = new Client($user);
+        $client->setName('Default client');
+
+        $this->getDoctrine()->getManager()->persist($client);
+
+        $user->addClient($client);
+
         $userManager->updateUser($user);
 
         // dispatch a created event so the associated config will be created
         $event = new UserEvent($user, $request);
         $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
 
-        return $this->sendUser($user, JsonResponse::HTTP_CREATED);
+        return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED);
     }
 
     /**
      * Send user response.
      *
-     * @param User $user
-     * @param int  $status HTTP Status code to send
+     * @param User   $user
+     * @param string $group  Used to define with serialized group might be used
+     * @param int    $status HTTP Status code to send
      *
      * @return JsonResponse
      */
-    private function sendUser(User $user, $status = JsonResponse::HTTP_OK)
+    private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK)
     {
         $json = $this->get('serializer')->serialize(
             $user,
             'json',
-            SerializationContext::create()->setGroups(['user_api'])
+            SerializationContext::create()->setGroups([$group])
         );
 
         return (new JsonResponse())