Skip to content

Commit

Permalink
Adding variable types and doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Arno2005 committed Nov 1, 2024
1 parent 7d54307 commit e4f578c
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 35 deletions.
11 changes: 7 additions & 4 deletions src/Libraries/Storage/Adapters/Dropbox/DropboxApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ class DropboxApp
/**
* @var string
*/
private $appKey = null;
private $appKey;

/**
* @var string
*/
private $appSecret = null;
private $appSecret;

/**
* @var TokenServiceInterface
*/
private $tokenService = null;
private $tokenService;

/**
* DropboxApp constructor
Expand All @@ -125,8 +125,11 @@ public function __construct(string $appKey, string $appSecret, TokenServiceInter

/**
* Gets the auth URL
* @throws CryptorException
* @param string $redirectUrl
* @param string $tokenAccessType
* @return string
* @throws AppException
* @throws CryptorException
* @throws DatabaseException
*/
public function getAuthUrl(string $redirectUrl, string $tokenAccessType = 'offline'): string
Expand Down
56 changes: 40 additions & 16 deletions src/Libraries/Storage/Adapters/GoogleDrive/GoogleDriveApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Quantum\Libraries\Storage\Adapters\GoogleDrive;

use Quantum\Exceptions\AppException;
use Quantum\Exceptions\CryptorException;
use Quantum\Exceptions\DatabaseException;
use Quantum\Libraries\Curl\HttpClient;
use Quantum\Http\Response;
use Exception;

class GoogleDriveApp
Expand Down Expand Up @@ -56,17 +58,17 @@ class GoogleDriveApp
/**
* @var string
*/
private $appKey = null;
private $appKey;

/**
* @var string
*/
private $appSecret = null;
private $appSecret;

/**
* @var TokenServiceInterface
*/
private $tokenService = null;
private $tokenService;

/**
* GoogleDriveApp constructor
Expand All @@ -83,7 +85,16 @@ public function __construct(string $appKey, string $appSecret, TokenServiceInter
$this->httpClient = $httpClient;
}

public function getAuthUrl($redirectUrl, $accessType = "offline"): string
/**
* Gets Auth URL
* @param string $redirectUrl
* @param string $accessType
* @return object|null
* @throws AppException
* @throws CryptorException
* @throws DatabaseException
*/
public function getAuthUrl(string $redirectUrl, string $accessType = "offline"): string
{
$params = [
'client_id' => $this->appKey,
Expand All @@ -97,7 +108,15 @@ public function getAuthUrl($redirectUrl, $accessType = "offline"): string
return self::AUTH_URL . '?' . http_build_query($params, '', '&');
}

public function fetchTokens($code, $redirectUrl = '', $byRefresh = false): ?object
/**
* Fetch tokens
* @param string $code
* @param string $redirectUrl
* @param bool $byRefresh
* @return object|null
* @throws Exception
*/
public function fetchTokens(string $code, string $redirectUrl = '', bool $byRefresh = false): ?object
{
$codeKey = $byRefresh ? 'refresh_token' : 'code';

Expand All @@ -112,16 +131,23 @@ public function fetchTokens($code, $redirectUrl = '', $byRefresh = false): ?obje
$params['redirect_uri'] = $redirectUrl;
}

$tokenUrl = self::AUTH_TOKEN_URL;

$response = $this->sendRequest($tokenUrl, $params);
$response = $this->sendRequest(self::AUTH_TOKEN_URL, $params);

$this->tokenService->saveTokens($response->access_token, !$byRefresh ? $response->refresh_token : null);

return $response;
}

public function sendRequest(string $uri, $data = null, array $headers = [], $method = 'POST')
/**
* Sends rpc request
* @param string $uri
* @param null $data
* @param array $headers
* @param string $method
* @return mixed|null
* @throws Exception
*/
public function sendRequest(string $uri, $data = null, array $headers = [], string $method = 'POST')
{
$this->httpClient
->createRequest($uri)
Expand All @@ -130,11 +156,9 @@ public function sendRequest(string $uri, $data = null, array $headers = [], $met
->setHeaders($headers)
->start();


$errors = $this->httpClient->getErrors();
$responseBody = $this->httpClient->getResponseBody();

if ($errors) {
if ($errors = $this->httpClient->getErrors()) {
$code = $errors['code'];

if ($code == self::INVALID_TOKEN_ERROR_CODE) {
Expand Down Expand Up @@ -167,7 +191,7 @@ public function sendRequest(string $uri, $data = null, array $headers = [], $met
* @return mixed|null
* @throws Exception
*/
public function rpcRequest(string $url, $params = [], $method = 'POST', $contentType = 'application/json')
public function rpcRequest(string $url, $params = [], string $method = 'POST', string $contentType = 'application/json')
{
try {
$headers = [
Expand All @@ -184,11 +208,11 @@ public function rpcRequest(string $url, $params = [], $method = 'POST', $content
* Gets file information
* @param string $fileId
* @param bool $media
* @param mixed $params
* @param array $params
* @return mixed|null
* @throws Exception
*/
public function getFileInfo(string $fileId, $media = false, $params = []){
public function getFileInfo(string $fileId, bool $media = false, array $params = []){
$queryParam = $media ? '?alt=media' : '?fields=*';
return $this->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $fileId . $queryParam, $params, 'GET');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class GoogleDriveFileSystemAdapter implements FilesystemAdapterInterface
*/
private static $instance = null;

/**
* @var GoogleDriveApp
*/
private $googleDriveApp;

/**
Expand All @@ -38,6 +41,9 @@ public static function getInstance(GoogleDriveApp $googleDriveApp): GoogleDriveF
return self::$instance;
}

/**
* @inheritDoc
*/
public function makeDirectory(string $dirname, ?string $parentId = null): bool
{
try{
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/Libraries/Storage/Adapters/Dropbox/DropboxAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,34 @@ class DropboxAppTest extends AppTestCase
use DropboxTokenServiceTestCase;
use HttpClientTestCase;

/**
* @var DropboxApp
*/
private $dropboxApp;

/**
* @var string
*/
private $appKey = 'x0hwm8wy63rrynm';

/**
* @var string
*/
private $appSecret = 'xxx123yyy';

/**
* @var string
*/
private $authCode = 'd4k29ovC7-UAAAAAAAA3Q45go2mLgjMhJSeJNBOo-EA';

/**
* @var string
*/
private $redirectUrl = 'http://localhost/confirm';

/**
* @var array
*/
private $tokensGrantResponse = [
"access_token" => "sl.BYEQ1_VadTz6nBU36WPBBVwokc3zWVMXGjcOKxV4Tadms8ZlEPM85aHVFa_k1sfjilCWOnl79RUncPZzJ3GhrqhLGIBFFRCH0rKMa_ZtcqkerJn-f5lu10Ki5PSw4fxYM80V4PL_",
"token_type" => "bearer",
Expand All @@ -32,6 +50,9 @@ class DropboxAppTest extends AppTestCase
"account_id" => "dbid:AAC9tDKbzTQlyNms0ZcB_iH3wLv7yNn-iyE"
];

/**
* @var array
*/
private $profileDataResponse = [
"account_id" => "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc",
"disabled" => false,
Expand All @@ -48,13 +69,19 @@ class DropboxAppTest extends AppTestCase
"profile_photo_url" => "https://dl-web.dropbox.com/account_photo/get/69330102&size=128x128"
];

/**
* @var array
*/
private $errorResponse = [
"error" => [
".tag" => "no_account"
],
"error_summary" => "no_account/..."
];

/**
* @var string
*/
private $fileContentResponse = 'Some plain text!';

public function setUp(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@

class DropboxFileSystemAdapterTest extends AppTestCase
{

/**
* @var DropboxFileSystemAdapter
*/
private $fs;

/**
* @var string
*/
private $dirname = 'common';

/**
* @var string
*/
private $filename = 'sample.txt';

/**
* @var string
*/
private $content = 'This file was created via dropbox api';

/**
* @var array
*/
private static $response = [];

public function setUp(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,57 @@ class GoogleDriveAppTest extends AppTestCase
use GoogleDriveTokenServiceTestCase;
use HttpClientTestCase;

/**
* @var GoogleDriveApp
*/
private $googleDriveApp;

private $appKey = 'x0hwm8wy63rrynm';
/**
* @var string
*/
private $appKey = 'x0hwm8wy63rrynm';

/**
* @var string
*/
private $appSecret = 'xxx123yyy';

/**
* @var string
*/
private $authCode = 'd4k29ovC7-UAAAAAAAA3Q45go2mLgjMhJSeJNBOo-EA';

/**
* @var string
*/
private $redirectUrl = 'http://localhost/confirm';

/**
* @var array
*/
private $tokensGrantResponse = [
"access_token" => "sl.BYEQ1_VadTz6nBU36WPBBVwokc3zWVMXGjcOKxV4Tadms8ZlEPM85aHVFa_k1sfjilCWOnl79RUncPZzJ3GhrqhLGIBFFRCH0rKMa_ZtcqkerJn-f5lu10Ki5PSw4fxYM80V4PL_",
"refresh_token" => "-3S067m3M5kAAAAAAAAAAcQF8zVqFUuhK-PFkFqiOfFTgiazWj5NyU-1EGWIh0ZS"
];

/**
* @var array
*/
private $fileMetadataResponse = [
"id" => "file1",
"kind" => GoogleDriveApp::DRIVE_FILE_KIND,
"name" => "myFile",
"mimeType" => "text/plain"
];

/**
* @var string
*/
private $fileContentResponse = 'Some plain text!';

/**
* @var string
*/
private $errorResponse = [
'code' => GoogleDriveApp::INVALID_TOKEN_ERROR_CODE,
'message' => 'Invalid access token',
Expand Down
Loading

0 comments on commit e4f578c

Please sign in to comment.