UploadCommandHandler::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9666
1
<?php
2
3
namespace diecoding\aws\s3\handlers;
4
5
use diecoding\aws\s3\commands\UploadCommand;
6
use diecoding\aws\s3\base\handlers\Handler;
7
use GuzzleHttp\Psr7\Utils;
8
use Psr\Http\Message\StreamInterface;
9
10
/**
11
 * Class UploadCommandHandler
12
 *
13
 * @package diecoding\aws\s3\handlers
14
 */
15
final class UploadCommandHandler extends Handler
16
{
17
    /**
18
     * @param \diecoding\aws\s3\commands\UploadCommand $command
19
     *
20
     * @return \Aws\ResultInterface|\GuzzleHttp\Promise\PromiseInterface
21
     */
22
    public function handle(UploadCommand $command)
23
    {
24
        $source = $this->sourceToStream($command->getSource());
25
        $options = array_filter($command->getOptions());
26
27
        $promise = $this->s3Client->uploadAsync(
28
            $command->getBucket(),
29
            $command->getFilename(),
30
            $source,
31
            $command->getAcl(),
32
            $options
33
        );
34
35
        return $command->isAsync() ? $promise : $promise->wait();
36
    }
37
38
    /**
39
     * Create a new stream based on the input type.
40
     *
41
     * @param resource|string|StreamInterface $source path to a local file, resource or stream
42
     *
43
     * @return StreamInterface
44
     */
45
    protected function sourceToStream($source): StreamInterface
46
    {
47
        if (is_string($source)) {
48
            $source = Utils::tryFopen($source, 'r+');
49
        }
50
51
        return Utils::streamFor($source);
52
    }
53
}
54