S3SignedResolver::resolve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Distribution\Resolver;
6
7
use Aws\CommandInterface;
8
use Aws\S3\S3ClientInterface;
9
use Psr\Http\Message\UriInterface;
10
use Spiral\Distribution\Internal\DateTimeIntervalFactoryInterface;
11
12
/**
13
 * @psalm-import-type DateIntervalFormat from DateTimeIntervalFactoryInterface
14
 * @see DateTimeIntervalFactoryInterface
15
 */
16
class S3SignedResolver extends ExpirationAwareResolver
17
{
18
    public function __construct(
19
        private readonly S3ClientInterface $client,
20
        private readonly string $bucket,
21
        private readonly ?string $prefix = null,
22
    ) {
23
        parent::__construct();
24
    }
25
26
    /**
27
     * @throws \Exception
28
     */
29
    public function resolve(string $file, mixed $expiration = null): UriInterface
30
    {
31
        $command = $this->createCommand($this->concat($file, $this->prefix));
32
33
        $request = $this->client->createPresignedRequest(
34
            $command,
35
            $this->getExpirationDateTime($expiration),
36
        );
37
38
        return $request->getUri();
39
    }
40
41
    private function createCommand(string $file): CommandInterface
42
    {
43
        return $this->client->getCommand('GetObject', [
44
            'Bucket' => $this->bucket,
45
            'Key'    => $file,
46
        ]);
47
    }
48
}
49