PlainCommandHandler::transformToAwsCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace diecoding\aws\s3\handlers;
4
5
use Aws\CommandInterface as AwsCommand;
6
use diecoding\aws\s3\base\handlers\Handler;
7
use diecoding\aws\s3\interfaces\commands\Asynchronous;
8
use diecoding\aws\s3\interfaces\commands\PlainCommand;
9
10
/**
11
 * Class PlainCommandHandler
12
 *
13
 * @package diecoding\aws\s3\handlers
14
 */
15
final class PlainCommandHandler extends Handler
16
{
17
    /**
18
     * @param \diecoding\aws\s3\interfaces\commands\PlainCommand $command
19
     *
20
     * @return \Aws\ResultInterface|\GuzzleHttp\Promise\PromiseInterface
21
     */
22
    public function handle(PlainCommand $command)
23
    {
24
        $awsCommand = $this->transformToAwsCommand($command);
25
26
        /** @var \GuzzleHttp\Promise\PromiseInterface $promise */
27
        $promise = $this->s3Client->executeAsync($awsCommand);
28
29
        return $this->commandIsAsync($command) ? $promise : $promise->wait();
30
    }
31
32
    /**
33
     * @param \diecoding\aws\s3\interfaces\commands\PlainCommand $command
34
     *
35
     * @return bool
36
     */
37
    protected function commandIsAsync(PlainCommand $command): bool
38
    {
39
        return $command instanceof Asynchronous && $command->isAsync();
40
    }
41
42
    /**
43
     * @param \diecoding\aws\s3\interfaces\commands\PlainCommand $command
44
     *
45
     * @return \Aws\CommandInterface
46
     */
47
    protected function transformToAwsCommand(PlainCommand $command): AwsCommand
48
    {
49
        $args = array_filter($command->toArgs());
50
51
        return $this->s3Client->getCommand($command->getName(), $args);
52
    }
53
}
54