AwsS3PresignedUrlResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resolve() 0 19 2
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
 * @author Albin Kerouanton <[email protected]>
12
 *
13
 * @see http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURL.html
14
 */
15
class AwsS3PresignedUrlResolver implements ResolverInterface
16
{
17
    /** @var S3Client */
18
    private $service;
19
20
    /** @var string */
21
    private $bucket;
22
23
    /** @var string */
24
    private $baseDir;
25
26
    /** @var \DateTimeInterface */
27
    private $expiresAt;
28
29
    /**
30
     * @param S3Client           $service   Could be the same as the one given to the adapter or any other S3 client.
31
     * @param string             $bucket    Same as the one given to adapter.
32
     * @param string             $baseDir   Same as the one given to adapter.
33
     * @param \DateTimeInterface $expiresAt Presigned links are valid for a certain amount time of time only.
34
     */
35
    public function __construct(S3Client $service, $bucket, $baseDir, \DateTimeInterface $expiresAt)
36
    {
37
        $this->service   = $service;
38
        $this->bucket    = $bucket;
39
        $this->baseDir   = trim($baseDir, '/');
40
        $this->expiresAt = $expiresAt;
41
    }
42
43
    /**
44
     * Resolves given object path into presigned request URI.
45
     *
46
     * @param string $path
47
     *
48
     * @return string
49
     */
50
    public function resolve($path)
51
    {
52
        // For AWS SDK v2
53
        if ($this->service instanceof \Aws\Common\Client\AbstractClient) {
0 ignored issues
show
Bug introduced by
The class Aws\Common\Client\AbstractClient does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
            return $this->service->getObjectUrl(
55
                $this->bucket,
56
                $this->computePath($path),
57
                $this->expiresAt
58
            );
59
        }
60
61
        // For AWS SDK v3
62
        $command = $this->service->getCommand('GetObject', [
63
            'Bucket' => $this->bucket,
64
            'Key'    => $this->computePath($path),
65
        ]);
66
67
        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...
68
    }
69
70
    /**
71
     * Appends baseDir to $key.
72
     *
73
     * @param string $key
74
     *
75
     * @return string
76
     */
77
    private function computePath($key)
78
    {
79
        return ltrim($this->baseDir . '/' . ltrim($key, '/'), '/');
80
    }
81
}
82