DeleteBucket   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 58.81%

Importance

Changes 0
Metric Value
wmc 6
eloc 17
dl 0
loc 50
ccs 10
cts 17
cp 0.5881
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 3 1
A handle() 0 27 5
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 DeleteBucket extends CommandHandler
20
{
21
    /**
22
     * Delete the entire bucket.
23
     * For a complete reference:
24
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html
25
     *
26
     * @param array $params
27
     *
28
     * @return bool
29
     * @throws Exception
30
     */
31 5
    public function handle(array $params = []): bool
32
    {
33 5
        $bucketName = $params[ 'bucket' ];
34
35 5
        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 5
            $this->client->clearBucket(['bucket' => $bucketName]);
43 5
            $delete = $this->client->getConn()->deleteBucket(['Bucket' => $bucketName]);
44
45 5
            if (($delete instanceof ResultInterface) and $delete[ '@metadata' ][ 'statusCode' ] === 204) {
46 5
                $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' was successfully deleted', $bucketName));
47
48 5
                return true;
49
            }
50
51
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong in deleting bucket \'%s\'', $bucketName), 'warning');
52
53
            return false;
54
        } catch (S3Exception $e) {
55
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
56
57
            throw $e;
58
        }
59
    }
60
61
    /**
62
     * @param array $params
63
     *
64
     * @return bool
65
     */
66 5
    public function validateParams(array $params = []): bool
67
    {
68 5
        return (isset($params[ 'bucket' ]));
69
    }
70
}
71