Passed
Push — master ( 77859c...e0031c )
by Mauro
02:25 queued 18s
created

GetBucketSize::validateParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 Aws\ResultInterface;
15
use Matecat\SimpleS3\Commands\CommandHandler;
16
17
class GetBucketSize extends CommandHandler
18
{
19
    /**
20
     * Get the total size of items in a bucket.
21
     *
22
     * @param array $params
23
     *
24
     * @return int|mixed
25
     * @throws \Exception
26
     */
27
    public function handle($params = [])
28
    {
29
        $bucketName = $params['bucket'];
30
        $size = 0;
31
32
        $items = $this->client->getItemsInABucket([
33
            'bucket' => $bucketName,
34
            'prefix' => (isset($params['prefix'])) ? $params['prefix'] : null,
35
            'hydrate' => true
36
        ]);
37
38
        /** @var ResultInterface $item */
39
        foreach ($items as $key => $item) {
40
            $size += $item['ContentLength'];
41
        }
42
43
        if (null !== $this->commandHandlerLogger) {
44
            $this->commandHandlerLogger->log($this, sprintf('Size of \'%s\' bucket was successfully obtained', $bucketName));
45
        }
46
47
        return $size;
48
    }
49
50
    /**
51
     * @param array $params
52
     *
53
     * @return bool
54
     */
55
    public function validateParams($params = [])
56
    {
57
        return isset($params['bucket']);
58
    }
59
}
60