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

GetBucketSize::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 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 4
    public function handle($params = [])
28
    {
29 4
        $bucketName = $params['bucket'];
30 4
        $size = 0;
31
32 4
        $items = $this->client->getItemsInABucket([
33 4
            'bucket' => $bucketName,
34 4
            'prefix' => (isset($params['prefix'])) ? $params['prefix'] : null,
35
            'hydrate' => true
36
        ]);
37
38
        /** @var ResultInterface $item */
39 4
        foreach ($items as $key => $item) {
40 3
            $size += $item['ContentLength'];
41
        }
42
43 4
        if (null !== $this->commandHandlerLogger) {
44 4
            $this->commandHandlerLogger->log($this, sprintf('Size of \'%s\' bucket was successfully obtained', $bucketName));
45
        }
46
47 4
        return $size;
48
    }
49
50
    /**
51
     * @param array $params
52
     *
53
     * @return bool
54
     */
55 4
    public function validateParams($params = [])
56
    {
57 4
        return isset($params['bucket']);
58
    }
59
}
60