From 7513b50f8f74c4c0936b9c3146296f1e4bd34bd9 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 3 Jun 2015 02:40:24 +0200 Subject: [PATCH] Use the new client class to consume API actions --- .../core/src/Forum/Actions/IndexAction.php | 27 ++++++++++++------- .../core/src/Forum/Actions/LoginAction.php | 23 ++++++++-------- framework/core/src/Support/Action.php | 5 ++++ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/framework/core/src/Forum/Actions/IndexAction.php b/framework/core/src/Forum/Actions/IndexAction.php index ade1e20ca..a671965d1 100644 --- a/framework/core/src/Forum/Actions/IndexAction.php +++ b/framework/core/src/Forum/Actions/IndexAction.php @@ -1,16 +1,27 @@ apiClient = $apiClient; + $this->actor = $actor; + } + public function render(Request $request, $params = []) { $config = DB::table('config')->whereIn('key', ['base_url', 'api_url', 'forum_title', 'welcome_title', 'welcome_message'])->lists('value', 'key'); @@ -24,13 +35,11 @@ class IndexAction extends HtmlAction 'token' => $request->getCookieParams()['flarum_remember'], ]; - $response = app('Flarum\Api\Actions\Users\ShowAction') - ->handle(new ApiRequest(['id' => $user->id], $this->actor)) - ->content->toArray(); + $response = $this->apiClient->send('Flarum\Api\Actions\Users\ShowAction', ['id' => $user->id]); - $data = [$response['data']]; - if (isset($response['included'])) { - $data = array_merge($data, $response['included']); + $data = [$response->data]; + if (isset($response->included)) { + $data = array_merge($data, $response->included); } } diff --git a/framework/core/src/Forum/Actions/LoginAction.php b/framework/core/src/Forum/Actions/LoginAction.php index 917325edc..8d90f424a 100644 --- a/framework/core/src/Forum/Actions/LoginAction.php +++ b/framework/core/src/Forum/Actions/LoginAction.php @@ -1,8 +1,8 @@ users = $users; + $this->apiClient = $apiClient; } public function handle(Request $request, $routeParams = []) { $params = array_only($request->getAttributes(), ['identification', 'password']); - /** @var \Psr\Http\Message\ResponseInterface $response */ - $response = app('Flarum\Api\Actions\TokenAction')->handle(new ApiRequest($params)); + $data = $this->apiClient->send('Flarum\Api\Actions\TokenAction', $params); - if ($response->getStatusCode() === 200) { - $data = json_decode($response->getBody()); + event(new UserLoggedIn($this->users->findOrFail($data->userId), $data->token)); - event(new UserLoggedIn($this->users->findOrFail($data->userId), $data->token)); - return $this->withRememberCookie($response, $data->token); - } - - return $response; + // TODO: The client needs to pass through exceptions + return $this->withRememberCookie( + $this->success(), + $data->token + ); } } diff --git a/framework/core/src/Support/Action.php b/framework/core/src/Support/Action.php index b87fe9970..419be1a95 100644 --- a/framework/core/src/Support/Action.php +++ b/framework/core/src/Support/Action.php @@ -20,6 +20,11 @@ abstract class Action return $action->call($params); } + protected function success() + { + return new Response(); + } + /** * @param string $url * @return \Psr\Http\Message\ResponseInterface