DeleteFolder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 23
dl 0
loc 58
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 33 7
A validateParams() 0 5 2
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 Matecat\SimpleS3\Commands\CommandHandler;
15
use Matecat\SimpleS3\Helpers\File;
16
17
class DeleteFolder extends CommandHandler
18
{
19
    /**
20
     * Delete a folder.
21
     * For a complete reference:
22
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html
23
     *
24
     * @param mixed $params
25
     *
26
     * @return bool
27
     * @throws \Exception
28
     */
29
    public function handle($params = [])
30
    {
31
        $bucketName = $params['bucket'];
32
        $prefix = $params['prefix'];
33
34
        if (false === File::endsWith($prefix, $this->client->getPrefixSeparator())) {
35
            $prefix .= $this->client->getPrefixSeparator();
36
        }
37
38
        try {
39
            $this->client->getConn()->deleteMatchingObjects($bucketName, $prefix);
40
            if (null !== $this->commandHandlerLogger) {
41
                $this->commandHandlerLogger->log($this, sprintf('Folder \'%s\' was successfully deleted from \'%s\' bucket', $prefix, $bucketName));
42
            }
43
44
            if ($this->client->hasCache()) {
45
                $items = $this->client->getItemsInABucket([
46
                    'bucket' => $bucketName,
47
                    'prefix' => $prefix,
48
                ]);
49
50
                foreach ($items as $key) {
51
                    $this->client->getCache()->remove($bucketName, $key);
52
                }
53
            }
54
55
            return true;
56
        } catch (\Exception $e) {
57
            if (null !== $this->commandHandlerLogger) {
58
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
59
            }
60
61
            throw $e;
62
        }
63
    }
64
65
    /**
66
     * @param array $params
67
     *
68
     * @return bool
69
     */
70
    public function validateParams($params = [])
71
    {
72
        return (
73
            isset($params['bucket']) and
74
            isset($params['prefix'])
75
        );
76
    }
77
}
78