EnableAcceleration::validateParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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