SetBucketVersioning   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 67.86%

Importance

Changes 0
Metric Value
wmc 9
eloc 26
dl 0
loc 66
ccs 19
cts 28
cp 0.6786
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 43 8
A validateParams() 0 3 1
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 SetBucketVersioning extends CommandHandler
19
{
20
    /**
21
     * Enable versioning for a bucket.
22
     * For a complete reference:
23
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-versioning.html?highlight=versioning%20bucket
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 1
        $config     = [
34 1
                'Bucket'                  => $bucketName,
35 1
                'VersioningConfiguration' => [
36 1
                        'MFADelete' => 'Disabled',
37 1
                        'Status'    => 'Enabled',
38 1
                ],
39 1
        ];
40
41 1
        if (isset($params[ 'md5' ])) {
42
            $config[ 'ContentMD5' ] = $params[ 'md5' ];
43
        }
44
45 1
        if (isset($params[ 'mfa' ])) {
46
            $config[ 'MFA' ] = $params[ 'mfa' ];
47
        }
48
49 1
        if (isset($params[ 'mfa_delete' ])) {
50
            $config[ 'VersioningConfiguration' ][ 'MFADelete' ] = $params[ 'mfa_delete' ];
51
        }
52
53 1
        if (isset($params[ 'status' ])) {
54
            $config[ 'VersioningConfiguration' ][ 'Status' ] = $params[ 'status' ];
55
        }
56
57
        try {
58 1
            $versioning = $this->client->getConn()->putBucketVersioning($config);
59
60 1
            if (($versioning instanceof ResultInterface) and $versioning[ '@metadata' ][ 'statusCode' ] === 200) {
61 1
                $this->commandHandlerLogger?->log($this, sprintf('Versioning was successfully enabled for \'%s\' bucket', $bucketName));
62
63 1
                return true;
64
            }
65
66
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong during versioning of bucket \'%s\'', $bucketName), 'warning');
67
68
            return false;
69
        } catch (Exception $e) {
70
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
71
72
            throw $e;
73
        }
74
    }
75
76
    /**
77
     * @param array $params
78
     *
79
     * @return bool
80
     */
81 1
    public function validateParams(array $params = []): bool
82
    {
83 1
        return isset($params[ 'bucket' ]);
84
    }
85
}
86