MAPG-177 refactor GoogleOAuth, Http\Request, Http\Response to be more testable

This commit is contained in:
Bence Pőcze 2020-06-26 00:02:24 +02:00
parent fd657b4244
commit c06dd1e1d2
6 changed files with 60 additions and 14 deletions

View File

@ -1,5 +1,6 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Http\Request;
use MapGuesser\Interfaces\Request\IRequest; use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\Interfaces\Response\IRedirect; use MapGuesser\Interfaces\Response\IRedirect;
@ -49,7 +50,7 @@ class LoginController
$this->request->session()->set('oauth_state', $state); $this->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()); $url = $oAuth->getDialogUrl($state, $this->request->getBase() . '/' . \Container::$routeCollection->getRoute('login-google-action')->generateLink());
return new Redirect($url, IRedirect::TEMPORARY); return new Redirect($url, IRedirect::TEMPORARY);
@ -147,7 +148,7 @@ class LoginController
return new HtmlContent('login/google_login', $data); 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()); $tokenData = $oAuth->getToken($this->request->query('code'), $this->request->getBase() . '/' . \Container::$routeCollection->getRoute('login-google-action')->generateLink());
if (!isset($tokenData['id_token'])) { if (!isset($tokenData['id_token'])) {

View File

@ -1,11 +1,10 @@
<?php namespace MapGuesser\Http; <?php namespace MapGuesser\Http;
class Request use MapGuesser\Interfaces\Http\IRequest;
use MapGuesser\Interfaces\Http\IResponse;
class Request implements IRequest
{ {
const HTTP_GET = 0;
const HTTP_POST = 1;
private string $url; private string $url;
private int $method; private int $method;
@ -14,12 +13,22 @@ class Request
private array $headers = []; private array $headers = [];
public function __construct(string $url, int $method = self::HTTP_GET) public function __construct(string $url = '', int $method = self::HTTP_GET)
{ {
$this->url = $url; $this->url = $url;
$this->method = $method; $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) public function setQuery($query)
{ {
if (is_string($query)) { if (is_string($query)) {
@ -34,7 +43,7 @@ class Request
$this->headers = array_merge($this->headers, $headers); $this->headers = array_merge($this->headers, $headers);
} }
public function send(): Response public function send(): IResponse
{ {
$ch = curl_init(); $ch = curl_init();

View File

@ -1,6 +1,8 @@
<?php namespace MapGuesser\Http; <?php namespace MapGuesser\Http;
class Response use MapGuesser\Interfaces\Http\IResponse;
class Response implements IResponse
{ {
private string $body; private string $body;

View File

@ -0,0 +1,18 @@
<?php namespace MapGuesser\Interfaces\Http;
interface IRequest
{
const HTTP_GET = 0;
const HTTP_POST = 1;
public function setUrl(string $url): void;
public function setMethod(int $method): void;
public function setQuery($query);
public function setHeaders(array $headers);
public function send(): IResponse;
}

View File

@ -0,0 +1,8 @@
<?php namespace MapGuesser\Interfaces\Http;
interface IResponse
{
public function getBody();
public function getHeaders();
}

View File

@ -1,6 +1,6 @@
<?php namespace MapGuesser\OAuth; <?php namespace MapGuesser\OAuth;
use MapGuesser\Http\Request; use MapGuesser\Interfaces\Http\IRequest;
class GoogleOAuth class GoogleOAuth
{ {
@ -8,6 +8,13 @@ class GoogleOAuth
private static $tokenUrlBase = 'https://oauth2.googleapis.com/token'; private static $tokenUrlBase = 'https://oauth2.googleapis.com/token';
private IRequest $request;
public function __construct(IRequest $request)
{
$this->request = $request;
}
public function getDialogUrl(string $state, string $redirectUrl): string public function getDialogUrl(string $state, string $redirectUrl): string
{ {
$oauthParams = [ $oauthParams = [
@ -32,9 +39,10 @@ class GoogleOAuth
'grant_type' => 'authorization_code', 'grant_type' => 'authorization_code',
]; ];
$request = new Request(self::$tokenUrlBase, Request::HTTP_POST); $this->request->setUrl(self::$tokenUrlBase);
$request->setQuery($tokenParams); $this->request->setMethod(IRequest::HTTP_POST);
$response = $request->send(); $this->request->setQuery($tokenParams);
$response = $this->request->send();
return json_decode($response->getBody(), true); return json_decode($response->getBody(), true);
} }