GetPublicItemLink   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
eloc 21
dl 0
loc 53
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 5 2
A handle() 0 29 6
1
<?php
2
/**
3
 *  This file is part of the Simple S3 package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Matecat\SimpleS3\Commands\Handlers;
13
14
use Psr\Http\Message\UriInterface;
15
use Matecat\SimpleS3\Commands\CommandHandler;
16
17
class GetPublicItemLink extends CommandHandler
18
{
19
    /**
20
     * Get the temporary public link of an item.
21
     * It return a presigned URL.
22
     *
23
     * @param array $params
24
     *
25
     * @return mixed|UriInterface
26
     * @throws \Exception
27
     */
28 2
    public function handle($params = [])
29
    {
30 2
        $bucketName = $params['bucket'];
31 2
        $keyName = $params['key'];
32 2
        $expires = (isset($params['expires'])) ? $params['expires'] : '+1 hour';
33
34 2
        if ($this->client->hasEncoder()) {
35 2
            $keyName = $this->client->getEncoder()->encode($keyName);
36
        }
37
38
        try {
39 2
            $cmd = $this->client->getConn()->getCommand('GetObject', [
40 2
                'Bucket' => $bucketName,
41 2
                'Key'    => $keyName,
42
            ]);
43
44 2
            $link = $this->client->getConn()->createPresignedRequest($cmd, $expires)->getUri();
45
46 2
            if (null !== $this->commandHandlerLogger) {
47 2
                $this->commandHandlerLogger->log($this, sprintf('Public link of \'%s\' file was successfully obtained from \'%s\' bucket', $keyName, $bucketName));
48
            }
49
50 2
            return $link;
51
        } catch (\InvalidArgumentException $e) {
52
            if (null !== $this->commandHandlerLogger) {
53
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
54
            }
55
56
            throw $e;
57
        }
58
    }
59
60
    /**
61
     * @param array $params
62
     *
63
     * @return bool
64
     */
65 2
    public function validateParams($params = [])
66
    {
67
        return (
68 2
            isset($params['bucket']) and
69 2
            isset($params['key'])
70
        );
71
    }
72
}
73