SetBucketLifecycleConfiguration   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 23
dl 0
loc 59
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 34 7
A validateParams() 0 5 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 Exception;
16
use Matecat\SimpleS3\Commands\CommandHandler;
17
18
class SetBucketLifecycleConfiguration extends CommandHandler
19
{
20
    /**
21
     * Set bucket lifecycle configuration.
22
     * For a complete reference of bucket lifecycle rules:
23
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-lifecycle-configuration.html
24
     *
25
     * @param array $params
26
     *
27
     * @return bool
28
     * @throws Exception
29
     */
30
    public function handle($params = [])
31
    {
32
        $bucketName = $params['bucket'];
33
        $rules = $params['rules'];
34
35
        try {
36
            $settings = [
37
                'Bucket' => $bucketName,
38
                'LifecycleConfiguration' => [
39
                    'Rules' => $rules
40
                ]
41
            ];
42
43
            $config = $this->client->getConn()->putBucketLifecycleConfiguration($settings);
44
45
            if (($config instanceof ResultInterface) and $config['@metadata']['statusCode'] === 200) {
46
                if (null !== $this->commandHandlerLogger) {
47
                    $this->commandHandlerLogger->log($this, sprintf('Lifecycle was successfully set for bucket \'%s\'', $bucketName));
48
                }
49
50
                return true;
51
            }
52
53
            if (null !== $this->commandHandlerLogger) {
54
                $this->commandHandlerLogger->log($this, sprintf('Something went wrong during setting of lifecycle of \'%s\' bucket', $bucketName), 'warning');
55
            }
56
57
            return false;
58
        } catch (\Exception $e) {
59
            if (null !== $this->commandHandlerLogger) {
60
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
61
            }
62
63
            throw $e;
64
        }
65
    }
66
67
    /**
68
     * @param array $params
69
     *
70
     * @return bool
71
     */
72
    public function validateParams($params = [])
73
    {
74
        return (
75
            isset($params['bucket']) and
76
            isset($params['rules'])
77
        );
78
    }
79
}
80