Passed
Push — master ( 3a0aa4...dae184 )
by Domenico
03:53
created

EnableAcceleration   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 50
ccs 15
cts 20
cp 0.75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 3 1
A handle() 0 27 4
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 Aws\ResultInterface;
15
use Exception;
16
use Matecat\SimpleS3\Commands\CommandHandler;
17
18
class EnableAcceleration extends CommandHandler
19
{
20
    /**
21
     * Enable acceleration for a bucket.
22
     * For a complete reference:
23
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-accelerate-configuration.html
24
     *
25
     * @param array $params
26
     *
27
     * @return bool
28
     * @throws Exception
29
     */
30 1
    public function handle(array $params = []): bool
31
    {
32 1
        $bucketName = $params[ 'bucket' ];
33
34
        try {
35 1
            $accelerate = $this->client->getConn()->putBucketAccelerateConfiguration(
36 1
                    [
37 1
                            'AccelerateConfiguration' => [
38 1
                                    'Status' => 'Enabled',
39 1
                            ],
40 1
                            'Bucket'                  => $bucketName,
41 1
                    ]
42 1
            );
43
44 1
            if (($accelerate instanceof ResultInterface) and $accelerate[ '@metadata' ][ 'statusCode' ] === 200) {
45 1
                $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' was successfully set to transfer accelerated mode', $bucketName));
46
47 1
                return true;
48
            }
49
50
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong during setting of bucket \'%s\' to transfer accelerated mode', $bucketName), 'warning');
51
52
            return false;
53
        } catch (Exception $e) {
54
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
55
56
            throw $e;
57
        }
58
    }
59
60
    /**
61
     * @param array $params
62
     *
63
     * @return bool
64
     */
65 1
    public function validateParams(array $params = []): bool
66
    {
67 1
        return isset($params[ 'bucket' ]);
68
    }
69
}
70