Skip to content

Commit

Permalink
refactoring of commands. now they are semantic
Browse files Browse the repository at this point in the history
  • Loading branch information
frostealth committed Oct 7, 2016
1 parent 55fa650 commit b4d9605
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 129 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ $s3 = Yii::$app->get('s3');
/** @var \Aws\ResultInterface $result */
$result = $s3->commands()->get('filename.ext')->saveAs('/path/to/local/file.ext')->execute();
$result = $s3->commands()->put('filename.ext', 'body')->setContentType('text/plain')->execute();
$result = $s3->commands()->put('filename.ext', 'body')->withContentType('text/plain')->execute();
$result = $s3->commands()->delete('filename.ext')->execute();
$result = $s3->commands()->upload('filename.ext', '/path/to/local/file.ext')->setAcl('private')->execute();
$result = $s3->commands()->upload('filename.ext', '/path/to/local/file.ext')->withAcl('private')->execute();
$result = $s3->commands()->restore('filename.ext', $days = 7)->execute();
Expand Down Expand Up @@ -118,7 +118,7 @@ $s3 = Yii::$app->get('s3');
/** @var \frostealth\yii2\aws\s3\commands\GetCommand $command */
$command = $s3->create(GetCommand::class);
$command->setBucket('my-another-bucket')->setFilename('filename.ext')->saveAs('/path/to/local/file.ext');
$command->inBucket('my-another-bucket')->byFilename('filename.ext')->saveAs('/path/to/local/file.ext');
/** @var \Aws\ResultInterface $result */
$result = $s3->execute($command);
Expand Down Expand Up @@ -169,7 +169,7 @@ class MyCommand implements Command, HasBucket
return $this->bucket;
}
public function setBucket(string $bucket)
public function inBucket(string $bucket)
{
$this->bucket = $bucket;
Expand All @@ -181,7 +181,7 @@ class MyCommand implements Command, HasBucket
return $this->something;
}
public function setSomething(string $something)
public function withSomething(string $something)
{
$this->something = $something;
Expand Down Expand Up @@ -221,7 +221,7 @@ $s3 = Yii::$app->get('s3');
/** @var \app\components\s3\commands\MyCommand $command */
$command = $s3->create(MyCommand::class);
$command->setSomething('some value')->setOption('OptionName', 'value');
$command->withSomething('some value')->withOption('OptionName', 'value');
/** @var \Aws\ResultInterface $result */
$result = $s3->execute($command);
Expand All @@ -234,8 +234,8 @@ Custom plain command looks like this:
namespace app\components\s3\commands;
use frostealth\yii2\aws\s3\interfaces\HasBucket;
use frostealth\yii2\aws\s3\interfaces\PlainCommand;
use frostealth\yii2\aws\s3\interfaces\commands\HasBucket;
use frostealth\yii2\aws\s3\interfaces\commands\PlainCommand;
class MyPlainCommand implements PlainCommand, HasBucket
{
Expand All @@ -246,7 +246,7 @@ class MyPlainCommand implements PlainCommand, HasBucket
return $this->args['Bucket'] ?? '';
}
public function setBucket(string $bucket)
public function inBucket(string $bucket)
{
$this->args['Bucket'] = $bucket;
Expand All @@ -258,7 +258,7 @@ class MyPlainCommand implements PlainCommand, HasBucket
return $this->args['something'] ?? '';
}
public function setSomething($something)
public function withSomething($something)
{
$this->args['something'] = $something;
Expand All @@ -278,7 +278,7 @@ class MyPlainCommand implements PlainCommand, HasBucket
```
Any command can extend the `ExecutableCommand` class or implement the `Executable` interface that will
allow to execute this command immediately: `$command->setSomething('some value')->execute();`.
allow to execute this command immediately: `$command->withSomething('some value')->execute();`.
## License
Expand Down
4 changes: 2 additions & 2 deletions src/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public function build(string $className): interfaces\commands\Command
protected function prepareCommand(interfaces\commands\Command $command)
{
if ($command instanceof interfaces\commands\HasBucket) {
$command->setBucket($this->bucket);
$command->inBucket($this->bucket);
}

if ($command instanceof interfaces\commands\HasAcl) {
$command->setAcl($this->acl);
$command->withAcl($this->acl);
}
}
}
16 changes: 8 additions & 8 deletions src/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function get(string $filename): GetCommand
{
/** @var GetCommand $command */
$command = $this->builder->build(GetCommand::class);
$command->setFilename($filename);
$command->byFilename($filename);

return $command;
}
Expand All @@ -56,7 +56,7 @@ public function put(string $filename, $body): PutCommand
{
/** @var PutCommand $command */
$command = $this->builder->build(PutCommand::class);
$command->setFilename($filename)->setBody($body);
$command->withFilename($filename)->withBody($body);

return $command;
}
Expand All @@ -70,7 +70,7 @@ public function delete(string $filename): DeleteCommand
{
/** @var DeleteCommand $command */
$command = $this->builder->build(DeleteCommand::class);
$command->setFilename($filename);
$command->byFilename($filename);

return $command;
}
Expand All @@ -85,7 +85,7 @@ public function upload(string $filename, $source): UploadCommand
{
/** @var UploadCommand $command */
$command = $this->builder->build(UploadCommand::class);
$command->setFilename($filename)->setSource($source);
$command->withFilename($filename)->withSource($source);

return $command;
}
Expand All @@ -100,7 +100,7 @@ public function restore(string $filename, int $days): RestoreCommand
{
/** @var RestoreCommand $command */
$command = $this->builder->build(RestoreCommand::class);
$command->setFilename($filename)->setDays($days);
$command->byFilename($filename)->withLifetime($days);

return $command;
}
Expand All @@ -114,7 +114,7 @@ public function exist(string $filename): ExistCommand
{
/** @var ExistCommand $command */
$command = $this->builder->build(ExistCommand::class);
$command->setFilename($filename);
$command->byFilename($filename);

return $command;
}
Expand All @@ -128,7 +128,7 @@ public function getUrl(string $filename): GetUrlCommand
{
/** @var GetUrlCommand $command */
$command = $this->builder->build(GetUrlCommand::class);
$command->setFilename($filename);
$command->byFilename($filename);

return $command;
}
Expand All @@ -143,7 +143,7 @@ public function getPresignedUrl(string $filename, $expires): GetPresignedUrlComm
{
/** @var GetPresignedUrlCommand $command */
$command = $this->builder->build(GetPresignedUrlCommand::class);
$command->setFilename($filename)->setExpires($expires);
$command->byFilename($filename)->withExpiration($expires);

return $command;
}
Expand Down
8 changes: 4 additions & 4 deletions src/base/commands/traits/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ trait Options
*
* @return $this
*/
final public function setOptions(array $value)
final public function withOptions(array $value)
{
$this->options = $value;

return $this;
}

Expand All @@ -38,10 +38,10 @@ final public function getOptions(): array
*
* @return $this
*/
final public function setOption(string $name, $value)
final public function withOption(string $name, $value)
{
$this->options[$name] = $value;

return $this;
}
}
14 changes: 9 additions & 5 deletions src/commands/DeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public function getBucket(): string
}

/**
* @param string $bucket
* @param string $name
*
* @return $this
*/
public function setBucket(string $bucket)
public function inBucket(string $name)
{
$this->args['Bucket'] = $bucket;
$this->args['Bucket'] = $name;

return $this;
}
Expand All @@ -59,7 +59,7 @@ public function getFilename(): string
*
* @return $this
*/
public function setFilename(string $filename)
public function byFilename(string $filename)
{
$this->args['Key'] = $filename;

Expand All @@ -79,14 +79,16 @@ public function getVersionId(): string
*
* @return $this
*/
public function setVersionId(string $versionId)
public function withVersionId(string $versionId)
{
$this->args['VersionId'] = $versionId;

return $this;
}

/**
* @internal used by the handlers
*
* @return string
*/
public function getName(): string
Expand All @@ -95,6 +97,8 @@ public function getName(): string
}

/**
* @internal used by the handlers
*
* @return array
*/
public function toArgs(): array
Expand Down
14 changes: 7 additions & 7 deletions src/commands/ExistCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class ExistCommand extends ExecutableCommand implements HasBucket
{
use Options;

/** @var string */
protected $bucket;

Expand All @@ -32,14 +32,14 @@ public function getBucket(): string
}

/**
* @param string $bucket
* @param string $name
*
* @return $this
*/
public function setBucket(string $bucket)
public function inBucket(string $name)
{
$this->bucket = $bucket;
$this->bucket = $name;

return $this;
}

Expand All @@ -56,10 +56,10 @@ public function getFilename(): string
*
* @return $this
*/
public function setFilename(string $filename)
public function byFilename(string $filename)
{
$this->filename = $filename;

return $this;
}
}
14 changes: 9 additions & 5 deletions src/commands/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public function getBucket(): string
}

/**
* @param string $bucket
* @param string $name
*
* @return $this
*/
public function setBucket(string $bucket)
public function inBucket(string $name)
{
$this->args['Bucket'] = $bucket;
$this->args['Bucket'] = $name;

return $this;
}
Expand All @@ -59,7 +59,7 @@ public function getFilename(): string
*
* @return $this
*/
public function setFilename(string $filename)
public function byFilename(string $filename)
{
$this->args['Key'] = $filename;

Expand All @@ -86,11 +86,13 @@ public function saveAs(string $value)
public function ifMatch(string $ifMatch)
{
$this->args['IfMatch'] = $ifMatch;

return $this;
}

/**
* @internal used by the handlers
*
* @return string
*/
public function getName(): string
Expand All @@ -99,6 +101,8 @@ public function getName(): string
}

/**
* @internal used by the handlers
*
* @return array
*/
public function toArgs(): array
Expand Down
Loading

0 comments on commit b4d9605

Please sign in to comment.