MAPG-177 refactor GoogleOAuth, Http\Request, Http\Response to be more testable
This commit is contained in:
parent
fd657b4244
commit
c06dd1e1d2
@ -1,5 +1,6 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Http\Request;
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\Interfaces\Response\IRedirect;
|
||||
@ -49,7 +50,7 @@ class LoginController
|
||||
|
||||
$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());
|
||||
|
||||
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'])) {
|
||||
|
@ -1,11 +1,10 @@
|
||||
<?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 int $method;
|
||||
@ -14,12 +13,22 @@ class Request
|
||||
|
||||
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->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();
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php namespace MapGuesser\Http;
|
||||
|
||||
class Response
|
||||
use MapGuesser\Interfaces\Http\IResponse;
|
||||
|
||||
class Response implements IResponse
|
||||
{
|
||||
private string $body;
|
||||
|
||||
|
18
src/Interfaces/Http/IRequest.php
Normal file
18
src/Interfaces/Http/IRequest.php
Normal 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;
|
||||
}
|
8
src/Interfaces/Http/IResponse.php
Normal file
8
src/Interfaces/Http/IResponse.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php namespace MapGuesser\Interfaces\Http;
|
||||
|
||||
interface IResponse
|
||||
{
|
||||
public function getBody();
|
||||
|
||||
public function getHeaders();
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php namespace MapGuesser\OAuth;
|
||||
|
||||
use MapGuesser\Http\Request;
|
||||
use MapGuesser\Interfaces\Http\IRequest;
|
||||
|
||||
class GoogleOAuth
|
||||
{
|
||||
@ -8,6 +8,13 @@ class GoogleOAuth
|
||||
|
||||
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
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user