Completed
Push — master ( 3a2d63...6346ac )
by Albin
03:04
created

AwsS3PresignedUrlResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resolve() 0 9 1
A computePath() 0 4 1
1
<?php
2
3
namespace Gaufrette\Extras\Resolvable\Resolver;
4
5
use Aws\S3\S3Client;
6
use Gaufrette\Extras\Resolvable\ResolverInterface;
7
8
/**
9
 * Resolves object paths into time-limited URLs, namely presigned URLs.
10
 *
11
 * @see http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURL.html
12
 */
13
class AwsS3PresignedUrlResolver implements ResolverInterface
14
{
15
    /** @var S3Client */
16
    private $service;
17
18
    /** @var string */
19
    private $bucket;
20
21
    /** @var string */
22
    private $baseDir;
23
24
    /** @var \DateTimeInterface */
25
    private $expiresAt;
26
27
    /**
28
     * @param S3Client           $service   Could be the same as the one given to the adapter or any other S3 client.
29
     * @param string             $bucket    Same as the one given to adapter.
30
     * @param string             $baseDir   Same as the one given to adapter.
31
     * @param \DateTimeInterface $expiresAt Presigned links are valid for a certain amount time of time only.
32
     */
33
    public function __construct(S3Client $service, $bucket, $baseDir, \DateTimeInterface $expiresAt)
34
    {
35
        $this->service   = $service;
36
        $this->bucket    = $bucket;
37
        $this->baseDir   = trim($baseDir, '/');
38
        $this->expiresAt = $expiresAt;
39
    }
40
41
    /**
42
     * Resolves given object path into presigned request URI.
43
     *
44
     * @param string $path
45
     *
46
     * @return (string) \Psr\Http\Message\UriInterface
47
     */
48
    public function resolve($path)
49
    {
50
        $command = $this->service->getCommand('GetObject', [
51
            'Bucket' => $this->bucket,
52
            'Key'    => $this->computePath($path),
53
        ]);
54
55
        return (string) $this->service->createPresignedRequest($command, $this->expiresAt)->getUri();
0 ignored issues
show
Documentation introduced by
$this->expiresAt is of type object<DateTimeInterface>, but the function expects a integer|string|object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
    }
57
58
    /**
59
     * Appends baseDir to $key.
60
     *
61
     * @param string $key
62
     *
63
     * @return string
64
     */
65
    private function computePath($key)
66
    {
67
        return ltrim($this->baseDir . '/' . ltrim($key, '/'), '/');
68
    }
69
}
70