From c06dd1e1d2d8cea22ff506411443a87ab9b08e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 26 Jun 2020 00:02:24 +0200 Subject: [PATCH] MAPG-177 refactor GoogleOAuth, Http\Request, Http\Response to be more testable --- src/Controller/LoginController.php | 5 +++-- src/Http/Request.php | 23 ++++++++++++++++------- src/Http/Response.php | 4 +++- src/Interfaces/Http/IRequest.php | 18 ++++++++++++++++++ src/Interfaces/Http/IResponse.php | 8 ++++++++ src/OAuth/GoogleOAuth.php | 16 ++++++++++++---- 6 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 src/Interfaces/Http/IRequest.php create mode 100644 src/Interfaces/Http/IResponse.php diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 82592c2..fb5573a 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -1,5 +1,6 @@ request->session()->set('oauth_state', $state); - $oAuth = new GoogleOAuth(); + $oAuth = new GoogleOAuth(new Request()); $url = $oAuth->getDialogUrl($state, $this->request->getBase() . '/' . \Container::$routeCollection->getRoute('login-google-action')->generateLink()); return new Redirect($url, IRedirect::TEMPORARY); @@ -147,7 +148,7 @@ class LoginController return new HtmlContent('login/google_login', $data); } - $oAuth = new GoogleOAuth(); + $oAuth = new GoogleOAuth(new Request()); $tokenData = $oAuth->getToken($this->request->query('code'), $this->request->getBase() . '/' . \Container::$routeCollection->getRoute('login-google-action')->generateLink()); if (!isset($tokenData['id_token'])) { diff --git a/src/Http/Request.php b/src/Http/Request.php index 2e5426a..9f1a05b 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -1,11 +1,10 @@ url = $url; $this->method = $method; } + public function setUrl(string $url): void + { + $this->url = $url; + } + + public function setMethod(int $method): void + { + $this->method = $method; + } + public function setQuery($query) { if (is_string($query)) { @@ -34,7 +43,7 @@ class Request $this->headers = array_merge($this->headers, $headers); } - public function send(): Response + public function send(): IResponse { $ch = curl_init(); diff --git a/src/Http/Response.php b/src/Http/Response.php index 9bee447..65958b0 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -1,6 +1,8 @@ request = $request; + } + public function getDialogUrl(string $state, string $redirectUrl): string { $oauthParams = [ @@ -32,9 +39,10 @@ class GoogleOAuth 'grant_type' => 'authorization_code', ]; - $request = new Request(self::$tokenUrlBase, Request::HTTP_POST); - $request->setQuery($tokenParams); - $response = $request->send(); + $this->request->setUrl(self::$tokenUrlBase); + $this->request->setMethod(IRequest::HTTP_POST); + $this->request->setQuery($tokenParams); + $response = $this->request->send(); return json_decode($response->getBody(), true); }