79 lines
2.7 KiB
PHP
79 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Console;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Modules\Helpdesk\Services\HelpdeskSyncService;
|
|
use Throwable;
|
|
|
|
final class HelpdeskSyncCommand extends Command
|
|
{
|
|
protected $signature = 'helpdesk:sync
|
|
{direction=pull : pull or push}
|
|
{--url= : Override HELPDESK_SYNC_REMOTE_URL}
|
|
{--token= : Override HELPDESK_SYNC_REMOTE_TOKEN}
|
|
{--settings-only : Sync Helpdesk settings/configuration only}
|
|
{--without-files : Do not transfer attachment file contents}
|
|
{--mirror : Delete local/remote Helpdesk rows that are absent from the source snapshot}';
|
|
|
|
protected $description = 'Synchronise Helpdesk data and settings with another Helpdesk host';
|
|
|
|
public function handle(HelpdeskSyncService $sync): int
|
|
{
|
|
$direction = strtolower((string) $this->argument('direction'));
|
|
|
|
if (! in_array($direction, ['push', 'pull'], true)) {
|
|
$this->error('Direction must be "push" or "pull".');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$url = trim((string) ($this->option('url') ?: config('helpdesk.sync.remote_url')));
|
|
$token = trim((string) ($this->option('token') ?: config('helpdesk.sync.remote_token')));
|
|
|
|
if ($url === '' || $token === '') {
|
|
$this->error('Configure HELPDESK_SYNC_REMOTE_URL and HELPDESK_SYNC_REMOTE_TOKEN, or pass --url and --token.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$settingsOnly = (bool) $this->option('settings-only');
|
|
$includeFiles = ! (bool) $this->option('without-files');
|
|
$mirror = (bool) $this->option('mirror');
|
|
|
|
$this->info(sprintf(
|
|
'%s Helpdesk %s %s',
|
|
ucfirst($direction),
|
|
$settingsOnly ? 'settings' : 'settings + data',
|
|
$direction === 'push' ? 'to '.$url : 'from '.$url,
|
|
));
|
|
|
|
if ($mirror) {
|
|
$this->warn('Mirror mode is enabled. Destination Helpdesk rows absent from the source can be deleted.');
|
|
}
|
|
|
|
try {
|
|
$result = $direction === 'push'
|
|
? $sync->push($url, $token, $settingsOnly, $includeFiles, $mirror)
|
|
: $sync->pull($url, $token, $settingsOnly, $includeFiles, $mirror);
|
|
} catch (Throwable $exception) {
|
|
$this->error($exception->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->table(
|
|
['Metric', 'Value'],
|
|
collect($result)->map(fn ($value, $key): array => [
|
|
(string) $key,
|
|
is_scalar($value) || $value === null ? (string) $value : json_encode($value, JSON_UNESCAPED_SLASHES),
|
|
])->values()->all(),
|
|
);
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|