Test Failed
Branch master (3a0aa4)
by Domenico
16:55
created

CreateBucketIfItDoesNotExist::handle()   B

Complexity

Conditions 10
Paths 29

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 42.0294

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 40
ccs 6
cts 19
cp 0.3158
rs 7.6666
c 0
b 0
f 0
cc 10
nc 29
nop 1
crap 42.0294

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 2
     */
34
    public function handle(array $params = []): bool
35 2
    {
36
        $bucketName = $params[ 'bucket' ];
37 2
38 1
        if (false === S3BucketNameValidator::isValid($bucketName)) {
39
            throw new InvalidS3NameException(sprintf('%s is not a valid bucket name. [' . implode(', ', S3BucketNameValidator::validate($bucketName)) . ']', $bucketName));
40
        }
41 1
42
        if (true === $this->client->hasBucket(['bucket' => $bucketName])) {
43
            $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' already exists', $bucketName), 'warning');
44
45
            return false;
46
        }
47
48
        try {
49
            $bucket = $this->client->getConn()->createBucket([
50 1
                    'Bucket' => $bucketName
51 1
            ]);
52
53
            if (isset($params[ 'rules' ]) and count($params[ 'rules' ]) > 0) {
54
                $this->client->setBucketLifecycleConfiguration(['bucket' => $bucketName, 'rules' => $params[ 'rules' ]]);
55
            }
56
57
            if (isset($params[ 'accelerate' ]) and true === $params[ 'accelerate' ]) {
58
                $this->client->enableAcceleration(['bucket' => $bucketName]);
59
            }
60
61
            if (($bucket instanceof ResultInterface) and $bucket[ '@metadata' ][ 'statusCode' ] === 200) {
62
                $this->commandHandlerLogger?->log($this, sprintf('Bucket \'%s\' was successfully created', $bucketName));
63
64
                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 1
    }
76 1
77 1
    /**
78
     * @param array $params
79
     *
80 1
     * @return bool
81
     */
82
    public function validateParams(array $params = []): bool
83
    {
84
        return isset($params[ 'bucket' ]);
85
    }
86
}
87