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

CreateBucketIfItDoesNotExist   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 44%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 0
loc 64
ccs 11
cts 25
cp 0.44
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 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