GetItemsInAVersionedBucket::handle()   C
last analyzed

Complexity

Conditions 13
Paths 87

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 38
ccs 0
cts 26
cp 0
rs 6.6166
c 0
b 0
f 0
cc 13
nc 87
nop 1
crap 182

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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