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