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

GetBucketSize   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 41
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 21 4
A validateParams() 0 3 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 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