DeleteBucketPolicy   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 35%

Importance

Changes 0
Metric Value
wmc 10
eloc 20
dl 0
loc 57
ccs 7
cts 20
cp 0.35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 3 1
B handle() 0 34 9
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 Aws\S3\Exception\S3Exception;
16
use Matecat\SimpleS3\Commands\CommandHandler;
17
18
class DeleteBucketPolicy extends CommandHandler
19
{
20
    /**
21
     * Delete the bucket policy.
22
     * For a complete reference:
23
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket-policy.html?highlight=delete%20policy
24
     *
25
     * @param mixed $params
26
     *
27
     * @return bool
28
     * @throws \Exception
29
     */
30 1
    public function handle($params = [])
31
    {
32 1
        $bucketName = $params['bucket'];
33
34 1
        if (false === $this->client->hasBucket(['bucket' => $bucketName])) {
35 1
            if (null !== $this->commandHandlerLogger) {
36
                $this->commandHandlerLogger->log($this, sprintf('Bucket \'%s\' does not exists', $bucketName), 'warning');
37
            }
38
39 1
            return false;
40
        }
41
42
        try {
43
            $delete = $this->client->getConn()->deleteBucketPolicy(['Bucket' => $bucketName]);
44
45
            if (($delete instanceof ResultInterface) and $delete['@metadata']['statusCode'] === 204) {
46
                if (null !== $this->commandHandlerLogger) {
47
                    $this->commandHandlerLogger->log($this, sprintf('Policy was successfully deleted 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 in deleting policy of bucket \'%s\'', $bucketName), 'warning');
55
            }
56
57
            return false;
58
        } catch (S3Exception $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 1
    public function validateParams($params = [])
73
    {
74 1
        return (isset($params['bucket']));
75
    }
76
}
77