2024-07-29 22:17:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Import\Quickbooks;
|
|
|
|
|
|
|
|
use App\Services\Import\Quickbooks\Contracts\SdkInterface as QuickbooksInterface;
|
|
|
|
|
|
|
|
final class SdkWrapper implements QuickbooksInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
const MAXRESULTS = 10000;
|
|
|
|
|
|
|
|
private $sdk;
|
|
|
|
private $entities = ['Customer','Invoice','Payment','Item'];
|
|
|
|
|
|
|
|
public function __construct($sdk)
|
|
|
|
{
|
|
|
|
// Prep Data Services
|
|
|
|
$this->sdk = $sdk;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAuthorizationUrl() : string
|
|
|
|
{
|
|
|
|
return ($this->sdk->getOAuth2LoginHelper())->getAuthorizationCodeURL();
|
2024-08-02 04:34:13 +02:00
|
|
|
}
|
2024-07-29 22:17:29 +02:00
|
|
|
|
2024-08-02 04:34:13 +02:00
|
|
|
public function getState() : string
|
|
|
|
{
|
|
|
|
return ($this->sdk->getOAuth2LoginHelper())->getState();
|
2024-07-29 22:17:29 +02:00
|
|
|
}
|
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
public function getAccessToken()
|
2024-07-29 22:17:29 +02:00
|
|
|
{
|
|
|
|
return $this->getTokens();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRefreshToken(): array{
|
2024-08-02 04:34:13 +02:00
|
|
|
return $this->getTokens();
|
2024-07-29 22:17:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function accessToken(string $code, string $realm) : array
|
|
|
|
{
|
|
|
|
$token = ($this->sdk->getOAuth2LoginHelper())->exchangeAuthorizationCodeForToken($code,$realm);
|
|
|
|
|
|
|
|
return $this->getTokens();
|
|
|
|
}
|
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
private function getTokens()
|
|
|
|
{
|
2024-07-29 22:17:29 +02:00
|
|
|
|
|
|
|
$token =($this->sdk->getOAuth2LoginHelper())->getAccessToken();
|
2024-08-22 00:14:54 +02:00
|
|
|
return $token;
|
2024-08-22 00:18:19 +02:00
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
// $access_token = $token->getAccessToken();
|
|
|
|
// $refresh_token = $token->getRefreshToken();
|
|
|
|
// $access_token_expires = $token->getAccessTokenExpiresAt();
|
|
|
|
// $refresh_token_expires = $token->getRefreshTokenExpiresAt();
|
2024-07-29 22:17:29 +02:00
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
// return compact('access_token', 'refresh_token','access_token_expires', 'refresh_token_expires');
|
2024-07-29 22:17:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function refreshToken(): array
|
|
|
|
{
|
|
|
|
$token = ($this->sdk->getOAuth2LoginHelper())->refreshToken();
|
|
|
|
$this->sdk = $this->sdk->updateOAuth2Token($token);
|
|
|
|
|
|
|
|
return $this->getTokens();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleCallbacks(array $data): void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function totalRecords(string $entity) : int {
|
|
|
|
return $this->sdk->Query("select count(*) from $entity");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function queryData(string $query, int $start = 1, $limit = 100) : array
|
|
|
|
{
|
|
|
|
return (array) $this->sdk->Query($query, $start, $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchRecords( string $entity, int $max = 1000): array {
|
|
|
|
|
|
|
|
if(!in_array($entity, $this->entities)) return [];
|
|
|
|
|
|
|
|
$records = [];
|
2024-07-30 20:19:27 +02:00
|
|
|
$start = 0;
|
2024-07-29 22:17:29 +02:00
|
|
|
$limit = 100;
|
|
|
|
try {
|
|
|
|
$total = $this->totalRecords($entity);
|
|
|
|
$total = min($max, $total);
|
|
|
|
|
|
|
|
// Step 3 & 4: Get chunks of records until the total required records are retrieved
|
|
|
|
do {
|
|
|
|
$limit = min(self::MAXRESULTS, $total - $start);
|
|
|
|
$recordsChunk = $this->queryData("select * from $entity", $start, $limit);
|
|
|
|
if(empty($recordsChunk)) break;
|
|
|
|
|
|
|
|
$records = array_merge($records,$recordsChunk);
|
|
|
|
$start += $limit;
|
|
|
|
} while ($start < $total);
|
2024-08-22 00:18:19 +02:00
|
|
|
if(empty($records)) throw new \Exception("No records retrieved!");
|
2024-07-29 22:17:29 +02:00
|
|
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
nlog("Fetch Quickbooks API Error: {$th->getMessage()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $records;
|
|
|
|
}
|
|
|
|
}
|