Skip to content

Commit

Permalink
Merge branch 'main' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Nov 15, 2024
2 parents 8ea4cf1 + ccda53c commit 451681c
Show file tree
Hide file tree
Showing 11 changed files with 881 additions and 702 deletions.
1,425 changes: 762 additions & 663 deletions build.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"sebastianfeldmann/ftp": "^0.9.2",
"guzzlehttp/guzzle": "^5.3.4|^6.5.8|^7.5.0",
"aws/aws-sdk-php": "^3.10",
"kunalvarma05/dropbox-php-sdk": "^0.4",
"kunalvarma05/dropbox-php-sdk": "^0.5",
"phpseclib/phpseclib": "^2.0",
"softlayer/objectstorage": "dev-master",
"vlucas/phpdotenv": "^4.0",
Expand Down
2 changes: 2 additions & 0 deletions doc/config/log/webhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"type": "webhook",
"options": {
"uri": "https://example.com/webhook",
"sendOnlyOnError": false,
"sendOnSimulation": false,
"username": "misterX",
"password": "secret",
"method": "POST",
Expand Down
6 changes: 6 additions & 0 deletions doc/config/log/webhook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<!-- mandatory -->
<option name="uri" value="https://some.host.com/webhook" />

<!-- optional default=false -->
<option name="sendOnlyOnError" value="true" />

<!-- optional default=true -->
<option name="sendOnSimulation" value="false" />

<!-- optional default=false -->
<option name="username" value="misterX" />

Expand Down
2 changes: 2 additions & 0 deletions doc/config/sync/dropbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"type": "dropbox",
"options": {
"token": "myCrazyLongApiTokenThatIGotFromDropbox",
"appKey": "myAppKey",
"appSecret": "myAppSecret",
"path": "/some/dir"
}
}
6 changes: 6 additions & 0 deletions doc/config/sync/dropbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<!-- mandatory -->
<option name="token" value="mycrazylongapitokenthatigotfromdropbox" />

<!-- mandatory -->
<option name="appKey" value="myAppKey" />

<!-- mandatory -->
<option name="appSecret" value="myAppSecret" />

<!-- mandatory -->
<option name="path" value="/some/dir" />
</sync>
7 changes: 3 additions & 4 deletions src/Backup/Collector/Dropbox.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
namespace phpbu\App\Backup\Collector;

use Kunnu\Dropbox\Dropbox as DropboxApi;
use Kunnu\Dropbox\Models\FolderMetadata;
use Kunnu\Dropbox as DropboxApi;
use phpbu\App\Backup\Collector;
use phpbu\App\Backup\File;
use phpbu\App\Backup\Path;
Expand Down Expand Up @@ -35,7 +34,7 @@ class Dropbox extends Remote implements Collector
* @param \phpbu\App\Backup\Path $path
* @param \Kunnu\Dropbox\Dropbox $client
*/
public function __construct(Target $target, Path $path, DropboxApi $client)
public function __construct(Target $target, Path $path, DropboxApi\Dropbox $client)
{
$this->setUp($target, $path);
$this->client = $client;

Check failure on line 40 in src/Backup/Collector/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

Property phpbu\App\Backup\Collector\Dropbox::$client (Kunnu\Dropbox) does not accept Kunnu\Dropbox\Dropbox.

Check failure on line 40 in src/Backup/Collector/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.1)

Property phpbu\App\Backup\Collector\Dropbox::$client (Kunnu\Dropbox) does not accept Kunnu\Dropbox\Dropbox.

Check failure on line 40 in src/Backup/Collector/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.3)

Property phpbu\App\Backup\Collector\Dropbox::$client (Kunnu\Dropbox) does not accept Kunnu\Dropbox\Dropbox.
Expand All @@ -55,7 +54,7 @@ protected function collectBackups()
/** @var \Kunnu\Dropbox\Models\FileMetadata $item */
foreach ($items->getItems() as $item) {
// skip directories
if ($item instanceof FolderMetadata) {
if ($item instanceof DropboxApi\Models\FolderMetadata) {
continue;
}
if ($this->isFileMatch($item->getPathDisplay())) {
Expand Down
8 changes: 4 additions & 4 deletions src/Backup/File/Dropbox.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace phpbu\App\Backup\File;

use Kunnu\Dropbox\Dropbox as DropboxApi;
use Kunnu\Dropbox\Models\FileMetadata;
use Kunnu\Dropbox as DropboxApi;
use phpbu\App\Exception;

/**
* Dropbox class.
Expand Down Expand Up @@ -31,7 +31,7 @@ class Dropbox extends Remote
* @param \Kunnu\Dropbox\Dropbox $client
* @param \Kunnu\Dropbox\Models\FileMetadata $dropboxFile
*/
public function __construct(DropboxApi $client, FileMetadata $dropboxFile)
public function __construct(DropboxApi\Dropbox $client, DropboxApi\Models\FileMetadata $dropboxFile)
{
$this->client = $client;
$this->filename = $dropboxFile->getName();
Expand All @@ -50,7 +50,7 @@ public function unlink()
try {
$this->client->delete($this->pathname);
} catch (\Exception $e) {
throw new \phpbu\App\Exception($e->getMessage());
throw new Exception($e->getMessage());
}
}
}
32 changes: 22 additions & 10 deletions src/Backup/Sync/Dropbox.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
namespace phpbu\App\Backup\Sync;

use Kunnu\Dropbox\DropboxApp as DropboxConfig;
use Kunnu\Dropbox\Dropbox as DropboxApi;
use Kunnu\Dropbox\DropboxFile;
use Kunnu\Dropbox as DropboxApi;
use Kunnu\Dropbox\Exceptions\DropboxClientException;
use phpbu\App\Backup\Collector;
use phpbu\App\Backup\Path;
use phpbu\App\Result;
Expand Down Expand Up @@ -61,6 +60,16 @@ class Dropbox implements Simulator
*/
protected $time;

/**
* @var string
*/
private mixed $appKey;

/**
* @var string
*/
private mixed $appSecret;

/**
* (non-PHPDoc)
*
Expand All @@ -76,10 +85,12 @@ public function setup(array $config)
}

// check for mandatory options
$this->validateConfig($config, ['token', 'path']);
$this->validateConfig($config, ['token', 'path', 'appKey', 'appSecret']);

$this->time = time();
$this->token = $config['token'];
$this->time = time();
$this->token = $config['token'];
$this->appKey = $config['appKey'];
$this->appSecret = $config['appSecret'];
// make sure the path contains a leading slash
$this->path = new Path(Util\Path::withLeadingSlash($config['path']), $this->time);

Expand Down Expand Up @@ -117,7 +128,7 @@ public function sync(Target $target, Result $result)
$client = $this->createClient();

try {
$file = new DropboxFile($sourcePath);
$file = new DropboxApi\DropboxFile($sourcePath);
$meta = $client->upload($file, $dropboxPath, ['autorename' => true]);
$result->debug('upload: done (' . $meta->getSize() . ')');

Expand Down Expand Up @@ -163,12 +174,13 @@ protected function createCollector(Target $target) : Collector
* Create a dropbox api client.
*
* @return \Kunnu\Dropbox\Dropbox
* @throws DropboxClientException
*/
protected function createClient() : DropboxApi
protected function createClient() : DropboxApi\Dropbox
{
if (!$this->client) {
$config = new DropboxConfig("id", "secret", $this->token);
$this->client = new DropboxApi($config);
$app = new DropboxApi\DropboxApp($this->appKey, $this->appSecret, $this->token);
$this->client = new DropboxApi\Dropbox($app);

Check failure on line 183 in src/Backup/Sync/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

Property phpbu\App\Backup\Sync\Dropbox::$client (Kunnu\Dropbox) does not accept Kunnu\Dropbox\Dropbox.

Check failure on line 183 in src/Backup/Sync/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.1)

Property phpbu\App\Backup\Sync\Dropbox::$client (Kunnu\Dropbox) does not accept Kunnu\Dropbox\Dropbox.

Check failure on line 183 in src/Backup/Sync/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.3)

Property phpbu\App\Backup\Sync\Dropbox::$client (Kunnu\Dropbox) does not accept Kunnu\Dropbox\Dropbox.
}
return $this->client;

Check failure on line 185 in src/Backup/Sync/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

Method phpbu\App\Backup\Sync\Dropbox::createClient() should return Kunnu\Dropbox\Dropbox but returns Kunnu\Dropbox.

Check failure on line 185 in src/Backup/Sync/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.1)

Method phpbu\App\Backup\Sync\Dropbox::createClient() should return Kunnu\Dropbox\Dropbox but returns Kunnu\Dropbox.

Check failure on line 185 in src/Backup/Sync/Dropbox.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.3)

Method phpbu\App\Backup\Sync\Dropbox::createClient() should return Kunnu\Dropbox\Dropbox but returns Kunnu\Dropbox.
}
Expand Down
33 changes: 32 additions & 1 deletion src/Log/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpbu\App\Result;
use phpbu\App\Util\Arr;
use phpbu\App\Util\Path;
use phpbu\App\Util\Str;
use Throwable;

/**
Expand Down Expand Up @@ -91,6 +92,20 @@ class Webhook implements Listener, Logger
'application/xml' => '\\phpbu\\App\\Log\\ResultFormatter\\Xml'
];

/**
* Call webhook only if errors occur
*
* @var bool
*/
private bool $sendOnlyOnError = false;

/**
* Call the webhook while simulating
*
* @var bool
*/
private bool $sendSimulating = false;

/**
* Constructor will only set the start time to be able to log duration
*/
Expand Down Expand Up @@ -140,6 +155,8 @@ public function setup(array $options)
throw new Exception('webhook URI is invalid');
}

$this->sendOnlyOnError = Str::toBoolean(Arr::getValue($options, 'sendOnlyOnError'), false);
$this->sendSimulating = Str::toBoolean(Arr::getValue($options, 'sendOnSimulation'), true);
$this->uri = $options['uri'];
$this->method = Arr::getValue($options, 'method', 'GET');
$this->username = Arr::getValue($options, 'username', '');
Expand All @@ -156,7 +173,10 @@ public function setup(array $options)
*/
public function onPhpbuEnd(Event\App\End $event)
{
$result = $event->getResult();
$result = $event->getResult();
if ($this->shouldWebhookBeCalled($result) === false) {
return;
}
$data = $this->getQueryStringData($result);
$uri = $this->method === 'GET' ? $this->buildGetUri($data) : $this->uri;
$formatter = $this->getBodyFormatter();
Expand Down Expand Up @@ -259,4 +279,15 @@ protected function fireRequest(string $uri, string $body = '')
throw new Exception('could not reach webhook: ' . $this->uri);
}
}

/**
* Should a webhook be called
*
* @param \phpbu\App\Result $result
* @return bool
*/
protected function shouldWebhookBeCalled(Result $result) : bool
{
return (!$this->sendOnlyOnError || !$result->allOk()) && ($this->sendSimulating || !$this->isSimulation);

Check failure on line 291 in src/Log/Webhook.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

Access to an undefined property phpbu\App\Log\Webhook::$isSimulation.

Check failure on line 291 in src/Log/Webhook.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.1)

Access to an undefined property phpbu\App\Log\Webhook::$isSimulation.

Check failure on line 291 in src/Log/Webhook.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.3)

Access to an undefined property phpbu\App\Log\Webhook::$isSimulation.
}
}
Loading

0 comments on commit 451681c

Please sign in to comment.