Skip to content

Commit

Permalink
Merge pull request #22 from gliterd/analysis-XZbavP
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
mhetreramesh authored Jan 11, 2019
2 parents 9ba55f2 + fad4a97 commit 42549be
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion phpunit.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

require __DIR__.'/vendor/autoload.php';
require __DIR__.'/vendor/autoload.php';
65 changes: 36 additions & 29 deletions src/BackblazeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Mhetreramesh\Flysystem;

use BackblazeB2\Client;
use GuzzleHttp\Psr7;
use League\Flysystem\Adapter\AbstractAdapter;
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
use League\Flysystem\Config;
use GuzzleHttp\Psr7;

class BackblazeAdapter extends AbstractAdapter {

class BackblazeAdapter extends AbstractAdapter
{
use NotSupportingVisibilityTrait;

protected $client;
Expand Down Expand Up @@ -37,9 +37,10 @@ public function write($path, $contents, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $contents
'FileName' => $path,
'Body' => $contents,
]);

return $this->getFileInfo($file);
}

Expand All @@ -50,9 +51,10 @@ public function writeStream($path, $resource, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $resource
'FileName' => $path,
'Body' => $resource,
]);

return $this->getFileInfo($file);
}

Expand All @@ -63,9 +65,10 @@ public function update($path, $contents, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $contents
'FileName' => $path,
'Body' => $contents,
]);

return $this->getFileInfo($file);
}

Expand All @@ -76,9 +79,10 @@ public function updateStream($path, $resource, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $resource
'FileName' => $path,
'Body' => $resource,
]);

return $this->getFileInfo($file);
}

Expand All @@ -89,11 +93,12 @@ public function read($path)
{
$file = $this->getClient()->getFile([
'BucketName' => $this->bucketName,
'FileName' => $path
'FileName' => $path,
]);
$fileContent = $this->getClient()->download([
'FileId' => $file->getId()
'FileId' => $file->getId(),
]);

return ['contents' => $fileContent];
}

Expand All @@ -105,15 +110,17 @@ public function readStream($path)
$stream = Psr7\stream_for();
$download = $this->getClient()->download([
'BucketName' => $this->bucketName,
'FileName' => $path,
'SaveAs' => $stream,
'FileName' => $path,
'SaveAs' => $stream,
]);
$stream->seek(0);

try {
$resource = Psr7\StreamWrapper::getResource($stream);
} catch (InvalidArgumentException $e) {
return false;
}

return $download === true ? ['stream' => $resource] : false;
}

Expand All @@ -132,8 +139,8 @@ public function copy($path, $newPath)
{
return $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $newPath,
'Body' => @file_get_contents($path)
'FileName' => $newPath,
'Body' => @file_get_contents($path),
]);
}

Expand All @@ -160,8 +167,8 @@ public function createDir($path, Config $config)
{
return $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => ''
'FileName' => $path,
'Body' => '',
]);
}

Expand Down Expand Up @@ -219,12 +226,12 @@ public function listContents($directory = '', $recursive = false)
]);
if ($recursive === true && $directory === '') {
$regex = '/^.*$/';
} else if ($recursive === true && $directory !== '') {
$regex = '/^' . preg_quote($directory) . '\/.*$/';
} else if ($recursive === false && $directory === '') {
} elseif ($recursive === true && $directory !== '') {
$regex = '/^'.preg_quote($directory).'\/.*$/';
} elseif ($recursive === false && $directory === '') {
$regex = '/^(?!.*\\/).*$/';
} else if ($recursive === false && $directory !== '') {
$regex = '/^' . preg_quote($directory) . '\/(?!.*\\/).*$/';
} elseif ($recursive === false && $directory !== '') {
$regex = '/^'.preg_quote($directory).'\/(?!.*\\/).*$/';
} else {
throw new \InvalidArgumentException();
}
Expand All @@ -234,24 +241,24 @@ public function listContents($directory = '', $recursive = false)
$normalized = array_map(function ($fileObject) {
return $this->getFileInfo($fileObject);
}, $fileObjects);

return array_values($normalized);
}

/**
* Get file info
* Get file info.
*
* @param $file
*
* @return array
*/

protected function getFileInfo($file)
{
$normalized = [
'type' => 'file',
'path' => $file->getName(),
'type' => 'file',
'path' => $file->getName(),
'timestamp' => substr($file->getUploadTimestamp(), 0, -3),
'size' => $file->getSize()
'size' => $file->getSize(),
];

return $normalized;
Expand Down
49 changes: 25 additions & 24 deletions tests/BackblazeAdapterTests.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use BackblazeB2\Client;
use Mhetreramesh\Flysystem\BackblazeAdapter as Backblaze;
use BackblazeB2\File;
use \League\Flysystem\Config;
use League\Flysystem\Config;
use Mhetreramesh\Flysystem\BackblazeAdapter as Backblaze;

class BackblazeAdapterTests extends PHPUnit_Framework_TestCase
{
Expand All @@ -17,7 +16,8 @@ class BackblazeAdapterTests extends PHPUnit_Framework_TestCase
*/
private $file_mock;

private function fileSetUp() {
private function fileSetUp()
{
$this->fs_mock = \org\bovigo\vfs\vfsStream::setup();
$this->file_mock = new \org\bovigo\vfs\vfsStreamFile('filename.ext');
$this->fs_mock->addChild($this->file_mock);
Expand All @@ -26,6 +26,7 @@ private function fileSetUp() {
public function backblazeProvider()
{
$mock = $this->prophesize('BackblazeB2\Client');

return [
[new Backblaze($mock->reveal(), 'my_bucket'), $mock],
];
Expand All @@ -36,7 +37,7 @@ public function backblazeProvider()
*/
public function testHas($adapter, $mock)
{
$mock->fileExists(["BucketName" => "my_bucket", "FileName" => "something"])->willReturn(true);
$mock->fileExists(['BucketName' => 'my_bucket', 'FileName' => 'something'])->willReturn(true);
$result = $adapter->has('something');
$this->assertTrue($result);
}
Expand All @@ -46,7 +47,7 @@ public function testHas($adapter, $mock)
*/
public function testWrite($adapter, $mock)
{
$mock->upload(["BucketName" => "my_bucket", "FileName" => "something", "Body" => "contents"])->willReturn(new File('something','','','',''), false);
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
$result = $adapter->write('something', 'contents', new Config());
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('type', $result);
Expand All @@ -58,7 +59,7 @@ public function testWrite($adapter, $mock)
*/
public function testWriteStream($adapter, $mock)
{
$mock->upload(["BucketName" => "my_bucket", "FileName" => "something", "Body" => "contents"])->willReturn(new File('something','','','',''), false);
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
$result = $adapter->writeStream('something', 'contents', new Config());
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('type', $result);
Expand All @@ -70,7 +71,7 @@ public function testWriteStream($adapter, $mock)
*/
public function testUpdate($adapter, $mock)
{
$mock->upload(["BucketName" => "my_bucket", "FileName" => "something", "Body" => "contents"])->willReturn(new File('something','','','',''), false);
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
$result = $adapter->update('something', 'contents', new Config());
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('type', $result);
Expand All @@ -82,7 +83,7 @@ public function testUpdate($adapter, $mock)
*/
public function testUpdateStream($adapter, $mock)
{
$mock->upload(["BucketName" => "my_bucket", "FileName" => "something", "Body" => "contents"])->willReturn(new File('something','','','',''), false);
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something', 'Body' => 'contents'])->willReturn(new File('something', '', '', '', ''), false);
$result = $adapter->updateStream('something', 'contents', new Config());
$this->assertInternalType('array', $result);
$this->assertArrayHasKey('type', $result);
Expand All @@ -94,9 +95,9 @@ public function testUpdateStream($adapter, $mock)
*/
public function testRead($adapter, $mock)
{
$file = new File('something','something4','','','','','my_bucket');
$mock->getFile(["BucketName" => "my_bucket", "FileName" => "something"])->willReturn($file, false);
$mock->download(["FileId" => "something"])->willReturn($file, false);
$file = new File('something', 'something4', '', '', '', '', 'my_bucket');
$mock->getFile(['BucketName' => 'my_bucket', 'FileName' => 'something'])->willReturn($file, false);
$mock->download(['FileId' => 'something'])->willReturn($file, false);
$result = $adapter->read('something');
$this->assertEquals(['contents' => $file], $result);
}
Expand Down Expand Up @@ -147,7 +148,7 @@ public function testGetMimetype($adapter, $mock)
public function testCopy($adapter, $mock)
{
$this->fileSetUp();
$mock->upload(["BucketName" => "my_bucket", "FileName" => "something_new", "Body" => ""])->willReturn(new File('something_new','','','',''), false);
$mock->upload(['BucketName' => 'my_bucket', 'FileName' => 'something_new', 'Body' => ''])->willReturn(new File('something_new', '', '', '', ''), false);
$result = $adapter->copy($this->file_mock->url(), 'something_new');
$this->assertObjectHasAttribute('id', $result, 'something_new');
}
Expand All @@ -157,25 +158,25 @@ public function testCopy($adapter, $mock)
*/
public function testListContents($adapter, $mock)
{
$mock->listFiles(["BucketName" => "my_bucket"])->willReturn([new File('random_id', 'file1.txt'), new File('random_id', 'some_folder/file2.txt'), new File('random_id', 'some_folder/another_folder/file3.txt')]);
$mock->listFiles(['BucketName' => 'my_bucket'])->willReturn([new File('random_id', 'file1.txt'), new File('random_id', 'some_folder/file2.txt'), new File('random_id', 'some_folder/another_folder/file3.txt')]);
$normalized_files = [
[
'type' => 'file',
'path' => 'file1.txt',
'type' => 'file',
'path' => 'file1.txt',
'timestamp' => false,
'size' => NULL,
'size' => null,
],
[
'type' => 'file',
'path' => 'some_folder/file2.txt',
'type' => 'file',
'path' => 'some_folder/file2.txt',
'timestamp' => false,
'size' => NULL,
'size' => null,
],
[
'type' => 'file',
'path' => 'some_folder/another_folder/file3.txt',
'type' => 'file',
'path' => 'some_folder/another_folder/file3.txt',
'timestamp' => false,
'size' => NULL,
'size' => null,
],
];
$result1 = $adapter->listContents('', false);
Expand All @@ -192,4 +193,4 @@ public function testListContents($adapter, $mock)
$adapter->listContents(false, 'haha');
$adapter->listContents('', 'haha');
}
}
}

0 comments on commit 42549be

Please sign in to comment.