CreateBucketIfItDoesNotExist::handle()   B
last analyzed

Complexity

Conditions 10
Paths 29

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11.1743

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 40
ccs 17
cts 22
cp 0.7727
rs 7.6666
c 0
b 0
f 0
cc 10
nc 29
nop 1
crap 11.1743

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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