Skip to content

Commit

Permalink
After @keksa CR
Browse files Browse the repository at this point in the history
  • Loading branch information
janprochazkacz committed Dec 9, 2024
1 parent f396c4d commit c4cc021
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 94 deletions.
Binary file modified plugins/backup-sync-dropbox/backup-sync-dropbox.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions plugins/backup-sync-dropbox/src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
20 changes: 10 additions & 10 deletions plugins/backup-sync-dropbox/src/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions plugins/backup-sync-dropbox/src/main.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use BackupSyncDropbox\Handler\BackupHandler;
use BackupSyncDropbox\Service\UnmsApiDropbox;
use BackupSyncDropbox\TokenProvider\DropboxTokenProvider;
use BackupSyncDropbox\Utility\LogCleaner;
use BackupSyncDropbox\Utility\Logger;
Expand All @@ -14,7 +15,6 @@
use Ubnt\UcrmPluginSdk\Service\PluginConfigManager;
use Ubnt\UcrmPluginSdk\Service\PluginLogManager;
use Ubnt\UcrmPluginSdk\Service\UcrmApi;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

require_once __DIR__ . '/vendor/autoload.php';

Expand Down Expand Up @@ -42,7 +42,7 @@
$builder->addDefinitions(
[
Filesystem::class => $filesystem,
UnmsApi::class => UnmsApi::create($unmsApiToken),
UnmsApiDropbox::class => UnmsApiDropbox::create($unmsApiToken),
UcrmApi::class => UcrmApi::create(),
PluginLogManager::class => $pluginLogManager,
LoggerInterface::class => $logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

/**
Expand All @@ -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;
Expand Down
23 changes: 17 additions & 6 deletions plugins/backup-sync-dropbox/src/src/Facade/BackupFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand All @@ -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));
}
Expand All @@ -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;
}
}
24 changes: 3 additions & 21 deletions plugins/backup-sync-dropbox/src/src/Handler/BackupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions plugins/backup-sync-dropbox/src/src/Service/UnmsApiDropbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace BackupSyncDropbox\Service;

use GuzzleHttp\RequestOptions;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

final class UnmsApiDropbox extends UnmsApi
{
public function getSink(string $endpoint, string $filePath): void
{
$this->request(
'GET',
$endpoint,
[
RequestOptions::SINK => $filePath,
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions plugins/backup-sync-dropbox/src/src/Utility/LogCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions plugins/backup-sync-dropbox/src/src/Utility/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions plugins/backup-sync-dropbox/src/src/Utility/NmsSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c4cc021

Please sign in to comment.