diff --git a/plugins/backup-sync-dropbox/backup-sync-dropbox.zip b/plugins/backup-sync-dropbox/backup-sync-dropbox.zip index 0d821c012..7a89b9552 100644 Binary files a/plugins/backup-sync-dropbox/backup-sync-dropbox.zip and b/plugins/backup-sync-dropbox/backup-sync-dropbox.zip differ diff --git a/plugins/backup-sync-dropbox/src/composer.json b/plugins/backup-sync-dropbox/src/composer.json index da7922872..0182fa1b5 100644 --- a/plugins/backup-sync-dropbox/src/composer.json +++ b/plugins/backup-sync-dropbox/src/composer.json @@ -8,11 +8,11 @@ }, "require": { "ext-mbstring": "*", - "ubnt/ucrm-plugin-sdk": "^0.9", "php-di/php-di": "^7.0", "psr/log": "^3.0", "league/flysystem": "^3.12", "spatie/flysystem-dropbox": "^3.0", - "stevenmaguire/oauth2-dropbox": "^3.1" + "stevenmaguire/oauth2-dropbox": "^3.1", + "ubnt/ucrm-plugin-sdk": "^0.12.0" } } diff --git a/plugins/backup-sync-dropbox/src/composer.lock b/plugins/backup-sync-dropbox/src/composer.lock index e7341d0dd..38f328367 100644 --- a/plugins/backup-sync-dropbox/src/composer.lock +++ b/plugins/backup-sync-dropbox/src/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "17131d29174bf5b396292477ba9a415a", + "content-hash": "00d0d9c4448cbcb7cd0c84f185574bf4", "packages": [ { "name": "graham-campbell/guzzle-factory", @@ -1695,30 +1695,30 @@ }, { "name": "ubnt/ucrm-plugin-sdk", - "version": "0.9.0", + "version": "0.12.0", "source": { "type": "git", "url": "https://github.com/Ubiquiti-App/UCRM-Plugin-SDK.git", - "reference": "02ca1d4ce7fca1bc7f49ef0259a03d0bfedec19f" + "reference": "4c69c1e8e6105d53bfed2a3c30da7dda144ede07" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Ubiquiti-App/UCRM-Plugin-SDK/zipball/02ca1d4ce7fca1bc7f49ef0259a03d0bfedec19f", - "reference": "02ca1d4ce7fca1bc7f49ef0259a03d0bfedec19f", + "url": "https://api.github.com/repos/Ubiquiti-App/UCRM-Plugin-SDK/zipball/4c69c1e8e6105d53bfed2a3c30da7dda144ede07", + "reference": "4c69c1e8e6105d53bfed2a3c30da7dda144ede07", "shasum": "" }, "require": { "ext-curl": "*", "ext-json": "*", - "guzzlehttp/guzzle": "^7.5", + "guzzlehttp/guzzle": "^7.8", "php": ">=8.1", - "symfony/filesystem": "^6.2" + "symfony/filesystem": "^6.3" }, "require-dev": { "eloquent/phony-phpunit": "^7.1", "eloquent/phpstan-phony": "^0.8.0", "ocramius/package-versions": "^2.7", - "php-coveralls/php-coveralls": "^2.5", + "php-coveralls/php-coveralls": "^2.6", "phpstan/phpstan": "^1.10", "phpstan/phpstan-strict-rules": "^1.2", "phpunit/phpunit": "^9.5", @@ -1748,9 +1748,9 @@ ], "support": { "issues": "https://github.com/Ubiquiti-App/UCRM-Plugin-SDK/issues", - "source": "https://github.com/Ubiquiti-App/UCRM-Plugin-SDK/tree/0.9.0" + "source": "https://github.com/Ubiquiti-App/UCRM-Plugin-SDK/tree/0.12.0" }, - "time": "2023-03-10T13:26:40+00:00" + "time": "2024-12-09T09:46:53+00:00" } ], "packages-dev": [], diff --git a/plugins/backup-sync-dropbox/src/main.php b/plugins/backup-sync-dropbox/src/main.php index ee66a647c..436ed41f9 100644 --- a/plugins/backup-sync-dropbox/src/main.php +++ b/plugins/backup-sync-dropbox/src/main.php @@ -1,6 +1,7 @@ addDefinitions( [ Filesystem::class => $filesystem, - UnmsApi::class => UnmsApi::create($unmsApiToken), + UnmsApiDropbox::class => UnmsApiDropbox::create($unmsApiToken), UcrmApi::class => UcrmApi::create(), PluginLogManager::class => $pluginLogManager, LoggerInterface::class => $logger, diff --git a/plugins/backup-sync-dropbox/src/src/DataProvider/BackupDataProvider.php b/plugins/backup-sync-dropbox/src/src/DataProvider/BackupDataProvider.php index fbb193f60..84ea4d196 100644 --- a/plugins/backup-sync-dropbox/src/src/DataProvider/BackupDataProvider.php +++ b/plugins/backup-sync-dropbox/src/src/DataProvider/BackupDataProvider.php @@ -5,27 +5,17 @@ namespace BackupSyncDropbox\DataProvider; use BackupSyncDropbox\Data\UnmsBackup; +use BackupSyncDropbox\Service\UnmsApiDropbox; use BackupSyncDropbox\Utility\NmsSettings; use DateTimeImmutable; use DateTimeZone; -use Ubnt\UcrmPluginSdk\Service\UnmsApi; final class BackupDataProvider { - /** - * @var UnmsApi - */ - private $unmsApi; - - /** - * @var NmsSettings - */ - private $nmsSettings; - - public function __construct(UnmsApi $unmsApi, NmsSettings $nmsSettings) - { - $this->unmsApi = $unmsApi; - $this->nmsSettings = $nmsSettings; + public function __construct( + private UnmsApiDropbox $unmsApiDropbox, + private NmsSettings $nmsSettings + ) { } /** @@ -36,7 +26,7 @@ public function getListOfUnmsBackups(): array $list = []; $nmsTimeZone = $this->nmsSettings->getTimeZone(); - $data = $this->unmsApi->get('nms/backups'); + $data = $this->unmsApiDropbox->get('nms/backups'); foreach ($data as $item) { if ($item['state'] !== 'success') { continue; diff --git a/plugins/backup-sync-dropbox/src/src/Facade/BackupFacade.php b/plugins/backup-sync-dropbox/src/src/Facade/BackupFacade.php index 516d827f1..afe886c65 100644 --- a/plugins/backup-sync-dropbox/src/src/Facade/BackupFacade.php +++ b/plugins/backup-sync-dropbox/src/src/Facade/BackupFacade.php @@ -5,14 +5,14 @@ namespace BackupSyncDropbox\Facade; use BackupSyncDropbox\Data\UnmsBackup; +use BackupSyncDropbox\Service\UnmsApiDropbox; use League\Flysystem\Filesystem; use Psr\Log\LoggerInterface; -use Ubnt\UcrmPluginSdk\Service\UnmsApi; final class BackupFacade { public function __construct( - private UnmsApi $unmsApi, + private UnmsApiDropbox $unmsApiDropbox, private Filesystem $filesystem, private LoggerInterface $logger ) { @@ -26,10 +26,11 @@ public function upload(UnmsBackup $unmsBackup): void return; } - $temp = tmpfile(); - fwrite($temp, $this->unmsApi->get(sprintf('nms/backups/%s', $unmsBackup->id))); - - $this->filesystem->writeStream($unmsBackup->filename, $temp); + $temporaryFile = $this->getTemporaryFile(); + $this->unmsApiDropbox->getSink(sprintf('nms/backups/%s', $unmsBackup->id), $temporaryFile); + $resource = fopen($temporaryFile, 'rb+'); + $this->filesystem->writeStream($unmsBackup->filename, $resource); + unlink($temporaryFile); $this->logger->info(sprintf('Uploaded file "%s".', $unmsBackup->filename)); } @@ -51,4 +52,14 @@ public function deleteExcept(array $filenames): void $this->logger->info(sprintf('Deleted file "%s".', $item['path'])); } } + + private function getTemporaryFile(): string + { + $tempDir = realpath(sys_get_temp_dir()); + assert(is_string($tempDir)); + $tmpFile = tempnam($tempDir, 'ucrmTmpFile'); + assert(is_string($tmpFile)); + + return $tmpFile; + } } diff --git a/plugins/backup-sync-dropbox/src/src/Handler/BackupHandler.php b/plugins/backup-sync-dropbox/src/src/Handler/BackupHandler.php index ee6e6cc52..339b255a3 100644 --- a/plugins/backup-sync-dropbox/src/src/Handler/BackupHandler.php +++ b/plugins/backup-sync-dropbox/src/src/Handler/BackupHandler.php @@ -10,29 +10,11 @@ final class BackupHandler { - /** - * @var LoggerInterface - */ - private $logger; - - /** - * @var BackupFacade - */ - private $backupFacade; - - /** - * @var BackupDataProvider - */ - private $backupDataProvider; - public function __construct( - LoggerInterface $logger, - BackupFacade $backupFacade, - BackupDataProvider $backupDataProvider + private LoggerInterface $logger, + private BackupFacade $backupFacade, + private BackupDataProvider $backupDataProvider ) { - $this->logger = $logger; - $this->backupFacade = $backupFacade; - $this->backupDataProvider = $backupDataProvider; } public function sync(): void diff --git a/plugins/backup-sync-dropbox/src/src/Service/UnmsApiDropbox.php b/plugins/backup-sync-dropbox/src/src/Service/UnmsApiDropbox.php new file mode 100644 index 000000000..5759bf9dd --- /dev/null +++ b/plugins/backup-sync-dropbox/src/src/Service/UnmsApiDropbox.php @@ -0,0 +1,22 @@ +request( + 'GET', + $endpoint, + [ + RequestOptions::SINK => $filePath, + ] + ); + } +} diff --git a/plugins/backup-sync-dropbox/src/src/TokenProvider/DropboxTokenProvider.php b/plugins/backup-sync-dropbox/src/src/TokenProvider/DropboxTokenProvider.php index b93a16bc6..075eae3bd 100644 --- a/plugins/backup-sync-dropbox/src/src/TokenProvider/DropboxTokenProvider.php +++ b/plugins/backup-sync-dropbox/src/src/TokenProvider/DropboxTokenProvider.php @@ -18,20 +18,10 @@ class DropboxTokenProvider implements TokenProvider { - /** - * @var PluginLogManager - */ - private $pluginLogManager; - - /** - * @var PluginConfigManager - */ - private $pluginConfigManager; - - public function __construct(PluginLogManager $pluginLogManager, PluginConfigManager $pluginConfigManager) - { - $this->pluginLogManager = $pluginLogManager; - $this->pluginConfigManager = $pluginConfigManager; + public function __construct( + private PluginLogManager $pluginLogManager, + private PluginConfigManager $pluginConfigManager + ) { } public function getToken(): string diff --git a/plugins/backup-sync-dropbox/src/src/Utility/LogCleaner.php b/plugins/backup-sync-dropbox/src/src/Utility/LogCleaner.php index 36d5984a8..5d3c86ff9 100644 --- a/plugins/backup-sync-dropbox/src/src/Utility/LogCleaner.php +++ b/plugins/backup-sync-dropbox/src/src/Utility/LogCleaner.php @@ -10,14 +10,8 @@ final class LogCleaner { private const MAX_LINES = 256; - /** - * @var PluginLogManager - */ - private $pluginLogManager; - - public function __construct(PluginLogManager $pluginLogManager) + public function __construct(private PluginLogManager $pluginLogManager) { - $this->pluginLogManager = $pluginLogManager; } public function clean(): void diff --git a/plugins/backup-sync-dropbox/src/src/Utility/Logger.php b/plugins/backup-sync-dropbox/src/src/Utility/Logger.php index c17f1f517..4f7cdb78e 100644 --- a/plugins/backup-sync-dropbox/src/src/Utility/Logger.php +++ b/plugins/backup-sync-dropbox/src/src/Utility/Logger.php @@ -13,14 +13,8 @@ final class Logger implements LoggerInterface { use LoggerTrait; - /** - * @var PluginLogManager - */ - private $pluginLogManager; - - public function __construct(PluginLogManager $pluginLogManager) + public function __construct(private PluginLogManager $pluginLogManager) { - $this->pluginLogManager = $pluginLogManager; } public function log($level, $message, array $context = []): void diff --git a/plugins/backup-sync-dropbox/src/src/Utility/NmsSettings.php b/plugins/backup-sync-dropbox/src/src/Utility/NmsSettings.php index ddcd33c2d..824af5b7a 100644 --- a/plugins/backup-sync-dropbox/src/src/Utility/NmsSettings.php +++ b/plugins/backup-sync-dropbox/src/src/Utility/NmsSettings.php @@ -4,26 +4,20 @@ namespace BackupSyncDropbox\Utility; +use BackupSyncDropbox\Service\UnmsApiDropbox; use DateTimeZone; use Exception; -use Ubnt\UcrmPluginSdk\Service\UnmsApi; final class NmsSettings { - /** - * @var UnmsApi - */ - private $unmsApi; - - public function __construct(UnmsApi $unmsApi) + public function __construct(private UnmsApiDropbox $unmsApiDropbox) { - $this->unmsApi = $unmsApi; } public function getTimeZone(): DateTimeZone { try { - $nmsSettings = $this->unmsApi->get('nms/settings'); + $nmsSettings = $this->unmsApiDropbox->get('nms/settings'); $timeZone = is_array($nmsSettings) ? ($nmsSettings['timezone'] ?? null) : null;