Skip to content

Commit

Permalink
Added wrappers for all metadata compatible methods (compose, uploadAs…
Browse files Browse the repository at this point in the history
…ync, getResumableUploader, getStreamableUploader)
  • Loading branch information
st0nebridge committed Jun 25, 2020
1 parent 5451ee5 commit 323d6be
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions Storage/MetaBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace TechDyn\GoogleStorageMetaBucket\Storage;

use Google\Cloud\Core\Upload\ResumableUploader;
use Google\Cloud\Core\Upload\StreamableUploader;
use Google\Cloud\Storage\Bucket;
use Google\Cloud\Storage\Connection\ConnectionInterface;
use Google\Cloud\Storage\StorageObject;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\StreamInterface;

class MetaBucket extends Bucket
{
/**
* @var array
*/
protected $metadata;

/**
Expand All @@ -20,7 +26,6 @@ class MetaBucket extends Bucket
public function __construct(ConnectionInterface $connection, $name, array $info = [])
{
parent::__construct($connection, $name, $info);

$this->metadata = [];
}

Expand All @@ -31,9 +36,48 @@ public function __construct(ConnectionInterface $connection, $name, array $info
*/
public function upload($data, array $options = [])
{
$metadata = $this->metadata ? [ 'metadata' => $this->metadata ] : [];
return parent::upload($data, $this->populateOptions($options));
}

return parent::upload($data, array_replace_recursive($metadata, $options));
/**
* @param StreamInterface|resource|string|null $data
* @param array $options
* @return PromiseInterface
*/
public function uploadAsync($data, array $options = [])
{
return parent::uploadAsync($data, $this->populateOptions($options));
}

/**
* @param array $sourceObjects
* @param string $name
* @param array $options
* @return StorageObject
*/
public function compose(array $sourceObjects, $name, array $options = [])
{
return parent::compose($sourceObjects, $name, $this->populateOptions($options));
}

/**
* @param StreamInterface|resource|string $data
* @param array $options
* @return StreamableUploader
*/
public function getStreamableUploader($data, array $options = [])
{
return parent::getStreamableUploader($data, $this->populateOptions($options));
}

/**
* @param StreamInterface|resource|string|null $data
* @param array $options
* @return ResumableUploader
*/
public function getResumableUploader($data, array $options = [])
{
return parent::getResumableUploader($data, $this->populateOptions($options));
}

/**
Expand All @@ -47,4 +91,16 @@ public function setOption(string $name, $value)

return $this;
}

/**
* @param $options
* @return array
*/
private function populateOptions($options)
{
return array_replace_recursive(
$this->metadata ?
[ 'metadata' => $this->metadata ] : []
, $options);
}
}

0 comments on commit 323d6be

Please sign in to comment.