Test Failed
Push — master ( a92e12...446184 )
by Domenico
04:27
created

ClearBucket::validateParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
16
class ClearBucket extends CommandHandler
17
{
18
    /**
19
     * Clear a bucket.
20
     *
21
     * @param array $params
22
     *
23
     * @return bool
24
     * @throws \Exception
25
     */
26 6
    public function handle($params = [])
27
    {
28 6
        $bucketName = $params['bucket'];
29 6
        $errors = [];
30
31 6
        if (false === $this->client->hasBucket(['bucket' => $bucketName])) {
32
            if (null !== $this->commandHandlerLogger) {
33
                $this->commandHandlerLogger->log($this, sprintf('Bucket \'%s\' does not exists', $bucketName), 'warning');
34
            }
35
36
            return false;
37
        }
38
39 6
        $items = $this->client->getItemsInABucket(['bucket' => $bucketName]);
40
41 6
        if (count($items) === 0) {
42 3
            return true;
43
        }
44
45 4
        foreach ($items as $key) {
46 4
            $version = null;
47 4
            if (strpos($key, '<VERSION_ID:') !== false) {
48 1
                $v = explode('<VERSION_ID:', $key);
49 1
                $version = str_replace('>', '', $v[1]);
50 1
                $key = $v[0];
51
            }
52
53 4
            if (false === $delete = $this->client->deleteItem(['bucket' => $bucketName, 'key' => $key, 'version' => $version])) {
54
                $errors[] = $delete;
55
            }
56
        }
57
58 4
        if (count($errors) === 0) {
59 4
            if (null !== $this->commandHandlerLogger) {
60 4
                $this->commandHandlerLogger->log($this, sprintf('Bucket \'%s\' was successfully cleared', $bucketName));
61
            }
62
63 4
            return true;
64
        }
65
66
        if (null !== $this->commandHandlerLogger) {
67
            $this->commandHandlerLogger->log($this, sprintf('Something went wrong while clearing bucket \'%s\'', $bucketName), 'warning');
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * @param array $params
75
     *
76
     * @return bool
77
     */
78 6
    public function validateParams($params = [])
79
    {
80 6
        return isset($params['bucket']);
81
    }
82
}
83