Passed
Push — master ( 77859c...e0031c )
by Mauro
02:25 queued 18s
created

SetBucketVersioning::validateParams()   A

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