Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration with B2 cloud storage
- Loading branch information
1 parent
ed1adf0
commit 6683fda
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace spec\BackupManager\Filesystems; | ||
|
||
use BackblazeB2\Http\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class BackblazeFilesystemSpec extends ObjectBehavior { | ||
|
||
function it_is_initializable() { | ||
$this->shouldHaveType('BackupManager\Filesystems\BackblazeFilesystem'); | ||
} | ||
|
||
function it_should_recognize_its_type_with_case_insensitivity() { | ||
foreach (['b2', 'B2'] as $type) { | ||
$this->handles($type)->shouldBe(true); | ||
} | ||
|
||
foreach ([null, 'foo'] as $type) { | ||
$this->handles($type)->shouldBe(false); | ||
} | ||
} | ||
|
||
function it_should_provide_an_instance_of_an_b2_filesystem() { | ||
$this->get($this->getConfig())->getAdapter()->shouldHaveType('Mhetreramesh\Flysystem\BackblazeAdapter'); | ||
} | ||
|
||
function getConfig() { | ||
return [ | ||
'key' => 'test_key', | ||
'accountId' => 'test_id', | ||
'bucket' => 'bucket', | ||
'options' => ['client' => $this->getMockClient()] | ||
]; | ||
} | ||
|
||
function getMockClient() { | ||
$handler = new HandlerStack(new MockHandler([$this->getMockAuthorizationResponse()])); | ||
|
||
return new Client(['handler' => $handler]); | ||
} | ||
|
||
function getMockAuthorizationResponse() { | ||
$body = '{ | ||
"accountId: "test_id", | ||
"apiUrl": "https://api900.backblaze.com", | ||
"authorizationToken": "testAuthToken, | ||
"downloadUrl": "https://f900.backblaze.com" | ||
}'; | ||
|
||
return new Response(200, [], $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php namespace BackupManager\Filesystems; | ||
|
||
use BackblazeB2\Client; | ||
use League\Flysystem\Filesystem as Flysystem; | ||
use Mhetreramesh\Flysystem\BackblazeAdapter; | ||
|
||
/** | ||
* Class BackblazeFilesystem | ||
* @package BackupManager\Filesystems | ||
*/ | ||
class BackblazeFilesystem implements Filesystem { | ||
|
||
/** | ||
* Test fitness of visitor. | ||
* @param $type | ||
* @return bool | ||
*/ | ||
public function handles($type) { | ||
return strtolower($type) == 'b2'; | ||
} | ||
|
||
/** | ||
* @param array $config | ||
* @return Flysystem | ||
*/ | ||
public function get(array $config) { | ||
if (!isset($config['options'])) { | ||
$config['options'] = []; | ||
} | ||
|
||
$client = new Client($config['accountId'], $config['key'], $config['options']); | ||
return new Flysystem(new BackblazeAdapter($client, $config['bucket'])); | ||
} | ||
} |