GetBucketSize::validateParams()   A
last analyzed

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