Skip to content

Commit

Permalink
Continue code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic committed Jan 24, 2023
1 parent 649bffa commit 0770288
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 32 deletions.
81 changes: 66 additions & 15 deletions src/Transfer/Destination.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ abstract class Destination
protected $resourceCache = [];

/**
* Internal Adapter State
* @var string
*/
protected string $endpoint = '';

/**
* Counters
*
* @var array $state
* @var array $counter
*/
protected $state = [];
protected $counters = [];

/**
* Constructor, mainly handles state initialization.
* Source
*
* Automatically detects if we are running within Swoole and uses a Swoole table instead of a PHP array.
* @var Source $source
*/
public function __construct(protected string $endpoint, protected string $projectID, protected string $key)
{
$this->state = [];
}
protected Source $source;

/**
* Gets the name of the adapter.
Expand All @@ -60,6 +62,27 @@ abstract public function getName(): string;
*/
abstract public function getSupportedResources(): array;

/**
* Get Source
*
* @return Source
*/
public function getSource(): Source {
return $this->source;
}

/**
* Set Soruce
*
* @param Source $source
*
* @returns self
*/
public function setSource(Source $source): self {
$this->source = $source;
return $this;
}

/**
* Register Logs Array
*
Expand All @@ -70,12 +93,38 @@ public function registerLogs(array &$logs): void {
}

/**
* Register Resource Cache
* Get Resource Counters
*
* @param string $resource
*
* @returns array
*/
public function &getCounter(string $resource): array {
if ($this->counters[$resource]) {
return $this->counters[$resource];
} else {
$this->counters[$resource] = [
'total' => 0,
'current' => 0,
'failed' => 0,
'skipped' => 0,
];

return $this->counters[$resource];
}
}

/**
* Register Transfer Hooks
*
* @param array &$cache
* @param array &$counters
*
* @return void
*/
public function registerResourceCache(array &$cache): void {
public function registerTransferHooks(array &$cache, array &$counters): void {
$this->resourceCache = &$cache;
$this->counters = &$counters;
}

/**
Expand All @@ -85,20 +134,21 @@ public function registerResourceCache(array &$cache): void {
* @param callable $callback
*/
public function run(array $resources, callable $callback, Source $source): void {
$this->source = $source;

foreach ($resources as $resource) {
if (!in_array($resource, $this->getSupportedResources())) {
$this->logs[Log::FATAL] = new Log("Cannot Transfer unsupported resource: '".$resource."'");
throw new \Exception("Cannot Transfer unsupported resource: '".$resource."'");
}

$source->run($resources, function (Log $currentLog, string $resourceType, array $resource) use ($callback) {
$source->run($resources, function (string $resourceType, array $resource) use ($callback) {
switch ($resourceType) {
case Transfer::RESOURCE_USERS: {
$this->importUsers($resource);
$this->importUsers($resource, $callback);
break;
}
}
$callback($currentLog, $resourceType, $resource);
});
}
}
Expand Down Expand Up @@ -234,8 +284,9 @@ protected function flatten(array $data, string $prefix = ''): array
* Import Users
*
* @param array $users
* @param callable $callback (Progress $progress)
*/
protected function importUsers(array $users): void {
protected function importUsers(array $users, callable $callback): void {
throw new \Exception("Not Implemented");
}
}
22 changes: 20 additions & 2 deletions src/Transfer/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use Utopia\Transfer\Destination;
use Utopia\Transfer\Resources\Hash;
use Utopia\Transfer\Log;
use Utopia\Transfer\Progress;
use Utopia\Transfer\Resources\User;
use Utopia\Transfer\Transfer;

class Appwrite extends Destination {
protected Client $client;

public function __construct(protected string $projectID, protected string $endpoint, private string $apiKey)
public function __construct(protected string $projectID, string $endpoint, private string $apiKey)
{
$this->client = new Client();
$this->client->setEndpoint($endpoint);
Expand Down Expand Up @@ -132,7 +133,7 @@ public function importPasswordUser(User $user): array|null
return $result;
}

public function importUsers(array $users): void
public function importUsers(array $users, callable $callback): void
{
$auth = new Users($this->client);

Expand All @@ -143,6 +144,8 @@ public function importUsers(array $users): void

if (!$createdUser) {
$this->logs[Log::ERROR][] = new Log('Failed to import user', \time(), $user);
$userCounters = &$this->getCounter(Transfer::RESOURCE_USERS);
$userCounters['failed']++;
} else {
// Add more data to the user
if ($user->getUsername()) {
Expand All @@ -166,9 +169,24 @@ public function importUsers(array $users): void
}

$this->logs[Log::SUCCESS][] = new Log('User imported successfully', \time(), $user);
$userCounters = &$this->getCounter(Transfer::RESOURCE_USERS);
$userCounters['current']++;

$callback(
new Progress(
Transfer::RESOURCE_USERS,
time(),
$userCounters['total'],
$userCounters['current'],
$userCounters['failed'],
$userCounters['skipped']
)
);
}
} catch (\Exception $e) {
$this->logs[Log::ERROR][] = new Log($e->getMessage(), \time(), $user);
$counter = &$this->getCounter(Transfer::RESOURCE_USERS);
$counter['failed']++;
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Transfer/Resources/Progress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Utopia\Transfer;

class Progress
{
function __construct(
private string $resourceType,
private int $timestamp,
private int $total = 0,
private int $current = 0,
private int $failed = 0,
private int $skipped = 0
){}
}
40 changes: 33 additions & 7 deletions src/Transfer/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ abstract class Source
protected $resourceCache = [];

/**
* Internal Adapter State
* Counters
*
* @var array $state
* @var array $counters
*/
protected $state = [];
protected $counters = [];

/**
* Endpoint
Expand All @@ -43,6 +43,28 @@ abstract class Source
*/
protected $endpoint = '';

/**
* Get Resource Counters
*
* @param string $resource
*
* @returns array
*/
public function &getCounter(string $resource): array {
if ($this->counters[$resource]) {
return $this->counters[$resource];
} else {
$this->counters[$resource] = [
'total' => 0,
'current' => 0,
'failed' => 0,
'skipped' => 0,
];

return $this->counters[$resource];
}
}

/**
* Gets the name of the adapter.
*
Expand All @@ -67,12 +89,16 @@ public function registerLogs(array &$logs): void {
}

/**
* Register Resource Cache
* Register Transfer Hooks
*
* @param array &$cache
* @param array &$counters
*
* @return void
*/
public function registerResourceCache(array &$cache): void {
public function registerTransferHooks(array &$cache, array &$counters): void {
$this->resourceCache = &$cache;
$this->counters = &$counters;
}

/**
Expand All @@ -89,9 +115,9 @@ public function run(array $resources, callable $callback): void {

switch ($resource) {
case Transfer::RESOURCE_USERS: {
$this->exportUsers(500, function (array $users) use ($callback) {
$this->exportUsers(100, function (array $users) use ($callback) {
$this->resourceCache = array_merge($this->resourceCache, $users);
$callback(new Log('Exporting Users...'), Transfer::RESOURCE_USERS, $users);
$callback(Transfer::RESOURCE_USERS, $users);
});
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Transfer/Sources/NHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function exportUsers(int $batchSize, callable $callback): void
$user['display_name'] ?? '',
new Hash($user['password_hash'], '', Hash::BCRYPT),
$user['phone_number'] ?? '',
$this->calculateTypes($user),
$this->calculateUserTypes($user),
'',
$user['email_verified'],
$user['phone_number_verified'],
Expand All @@ -78,7 +78,7 @@ public function exportUsers(int $batchSize, callable $callback): void
}
}

private function calculateTypes(array $user): array
private function calculateUserTypes(array $user): array
{
if (empty($user['password_hash']) && empty($user['phone_number']))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Transfer/Sources/Supabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function exportUsers(int $batchSize, callable $callback): void
'',
new Hash($user['encrypted_password'], '', Hash::BCRYPT),
$user['phone'] ?? '',
$this->calculateTypes($user),
$this->calculateAuthTypes($user),
'',
!empty($user['email_confirmed_at']),
!empty($user['phone_confirmed_at']),
Expand All @@ -78,7 +78,7 @@ public function exportUsers(int $batchSize, callable $callback): void
}
}

private function calculateTypes(array $user): array
private function calculateAuthTypes(array $user): array
{
if (empty($user['encrypted_password']) && empty($user['phone']))
{
Expand Down
Loading

0 comments on commit 0770288

Please sign in to comment.