DeleteBucketPolicy::handle()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.125

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 26
ccs 7
cts 14
cp 0.5
rs 9.4888
c 0
b 0
f 0
cc 5
nc 7
nop 1
crap 8.125
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 Exception;
17
use Matecat\SimpleS3\Commands\CommandHandler;
18
19
class DeleteBucketPolicy extends CommandHandler
20
{
21
    /**
22
     * Delete the bucket policy.
23
     * For a complete reference:
24
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket-policy.html?highlight=delete%20policy
25
     *
26
     * @param array $params
27
     *
28
     * @return bool
29
     * @throws Exception
30
     */
31 1
    public function handle(array $params = []): bool
32
    {
33 1
        $bucketName = $params[ 'bucket' ];
34
35 1
        if (false === $this->client->hasBucket(['bucket' => $bucketName])) {
36
            $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' does not exists', $bucketName), 'warning');
37
38
            return false;
39
        }
40
41
        try {
42 1
            $delete = $this->client->getConn()->deleteBucketPolicy(['Bucket' => $bucketName]);
43
44 1
            if (($delete instanceof ResultInterface) and $delete[ '@metadata' ][ 'statusCode' ] === 204) {
45 1
                $this->commandHandlerLogger?->log($this, sprintf('Policy was successfully deleted for bucket \'%s\'', $bucketName));
46
47 1
                return true;
48
            }
49
50
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong in deleting policy of bucket \'%s\'', $bucketName), 'warning');
51
52
            return false;
53
        } catch (S3Exception $e) {
54
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
55
56
            throw $e;
57
        }
58
    }
59
60
    /**
61
     * @param array $params
62
     *
63
     * @return bool
64
     */
65 1
    public function validateParams(array $params = []): bool
66
    {
67 1
        return (isset($params[ 'bucket' ]));
68
    }
69
}
70