GetItemsInAVersionedBucket   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
eloc 19
dl 0
loc 49
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 38 13
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\S3\Exception\S3Exception;
15
use Matecat\SimpleS3\Helpers\File;
16
17
class GetItemsInAVersionedBucket extends GetItemsInABucket
18
{
19
    /**
20
     * Get the list of keys in a versioned bucket.
21
     * If 'hydrate' parameter is set to true, an array of hydrated Aws\Result is returned instead.
22
     *
23
     * @param array $params
24
     *
25
     * @return array|mixed
26
     * @throws \Exception
27
     */
28
    public function handle($params = [])
29
    {
30
        $bucketName = $params['bucket'];
31
32
        try {
33
            $config = [
34
                'Bucket' => $bucketName,
35
            ];
36
37
            if (isset($params['prefix'])) {
38
39
                // add a final slash to prefix
40
                if (false === File::endsWith($params['prefix'], $this->client->getPrefixSeparator())) {
41
                    $params['prefix'] .= $this->client->getPrefixSeparator();
42
                }
43
44
                $config['Delimiter'] = (isset($params['delimiter'])) ? $params['delimiter'] : $this->client->getPrefixSeparator();
45
                $config['Prefix'] = $params['prefix'];
46
            }
47
48
            // 1. If 'exclude-cache' is set, return records always from S3
49
            if (isset($params['exclude-cache']) and true === $params['exclude-cache']) {
50
                return $this->returnItemsFromS3($bucketName, $config, (isset($params['hydrate'])) ? $params['hydrate'] : null);
51
            }
52
53
            // 2. If the cache is set and there is a prefix, return records from cache
54
            if ($this->client->hasCache() and isset($config['Prefix'])) {
55
                return $this->returnItemsFromCache($bucketName, $config, (isset($params['hydrate'])) ? $params['hydrate'] : null);
56
            }
57
58
            // 3. Otherwise, return records from S3
59
            return $this->returnVersionedItemsFromS3($bucketName, $config, (isset($params['hydrate'])) ? $params['hydrate'] : null);
60
        } catch (S3Exception $e) {
61
            if (null !== $this->commandHandlerLogger) {
62
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
63
            }
64
65
            throw $e;
66
        }
67
    }
68
}
69