CreateFolder::validateParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\Helpers\File;
18
19
class CreateFolder extends CommandHandler
20
{
21
    /**
22
     * Create a folder.
23
     * For a complete reference:
24
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html?highlight=put
25
     *
26
     * @param mixed $params
27
     *
28
     * @return bool
29
     * @throws \Exception
30
     */
31
    public function handle($params = [])
32
    {
33
        $bucketName = $params['bucket'];
34
        $keyName = $params['key'];
35
36
        if ($this->client->hasEncoder()) {
37
            $keyName = $this->client->getEncoder()->encode($keyName);
38
        }
39
40
        if (false === File::endsWith($keyName, $this->client->getPrefixSeparator())) {
41
            $keyName .= $this->client->getPrefixSeparator();
42
        }
43
44
        try {
45
            $folder = $this->client->getConn()->putObject([
46
                'Bucket' => $bucketName,
47
                'Key'    => $keyName,
48
                'Body'   => '',
49
//                'ACL'    => 'public-read' // only bucket owners are allowed to create folder with ACL or bucket owner enforcement must be disabled
50
            ]);
51
52
            if (($folder instanceof ResultInterface) and $folder['@metadata']['statusCode'] === 200) {
53
                if (null !== $this->commandHandlerLogger) {
54
                    $this->commandHandlerLogger->log($this, sprintf('Folder \'%s\' was successfully created in \'%s\' bucket', $keyName, $bucketName));
55
                }
56
57
                return true;
58
            }
59
60
            if (null !== $this->commandHandlerLogger) {
61
                $this->commandHandlerLogger->log($this, sprintf('Something went wrong during creation of \'%s\' folder inside \'%s\' bucket', $keyName, $bucketName), 'warning');
62
            }
63
64
            return false;
65
        } catch (S3Exception $e) {
66
            if (null !== $this->commandHandlerLogger) {
67
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
68
            }
69
70
            throw $e;
71
        }
72
    }
73
74
    /**
75
     * @param array $params
76
     *
77
     * @return bool
78
     */
79
    public function validateParams($params = [])
80
    {
81
        return (
82
            isset($params['bucket']) and
83
            isset($params['key'])
84
        );
85
    }
86
}
87