CreateBucketIfItDoesNotExist   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 0
loc 64
ccs 19
cts 24
cp 0.7917
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 3 1
B handle() 0 40 10
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
use Matecat\SimpleS3\Components\Validators\S3BucketNameValidator;
19
use Matecat\SimpleS3\Exceptions\InvalidS3NameException;
20
21
class CreateBucketIfItDoesNotExist extends CommandHandler
22
{
23
    /**
24
     * Create a bucket if it does not exists.
25
     * For a complete reference:
26
     *
27
     *
28
     *
29
     * @param array $params
30
     *
31
     * @return bool
32
     * @throws Exception
33
     */
34 16
    public function handle(array $params = []): bool
35
    {
36 16
        $bucketName = $params[ 'bucket' ];
37
38 16
        if (false === S3BucketNameValidator::isValid($bucketName)) {
39 1
            throw new InvalidS3NameException(sprintf('%s is not a valid bucket name. [' . implode(', ', S3BucketNameValidator::validate($bucketName)) . ']', $bucketName));
40
        }
41
42 15
        if (true === $this->client->hasBucket(['bucket' => $bucketName])) {
43 7
            $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' already exists', $bucketName), 'warning');
44
45 7
            return false;
46
        }
47
48
        try {
49 9
            $bucket = $this->client->getConn()->createBucket([
50 9
                    'Bucket' => $bucketName
51 9
            ]);
52
53 9
            if (isset($params[ 'rules' ]) and count($params[ 'rules' ]) > 0) {
54 1
                $this->client->setBucketLifecycleConfiguration(['bucket' => $bucketName, 'rules' => $params[ 'rules' ]]);
55
            }
56
57 9
            if (isset($params[ 'accelerate' ]) and true === $params[ 'accelerate' ]) {
58 1
                $this->client->enableAcceleration(['bucket' => $bucketName]);
59
            }
60
61 9
            if (($bucket instanceof ResultInterface) and $bucket[ '@metadata' ][ 'statusCode' ] === 200) {
62 9
                $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' was successfully created', $bucketName));
63
64 9
                return true;
65
            }
66
67
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong during creation of bucket \'%s\'', $bucketName), 'warning');
68
69
            return false;
70
        } catch (S3Exception $e) {
71
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
72
73
            throw $e;
74
        }
75
    }
76
77
    /**
78
     * @param array $params
79
     *
80
     * @return bool
81
     */
82 16
    public function validateParams(array $params = []): bool
83
    {
84 16
        return isset($params[ 'bucket' ]);
85
    }
86
}
87