RestoreItem::handle()   B
last analyzed

Complexity

Conditions 10
Paths 80

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 10.5498

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 50
ccs 28
cts 34
cp 0.8235
rs 7.6666
c 0
b 0
f 0
cc 10
nc 80
nop 1
crap 10.5498

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\ResultInterface;
15
use Exception;
16
use InvalidArgumentException;
17
use Matecat\SimpleS3\Commands\CommandHandler;
18
19
class RestoreItem extends CommandHandler
20
{
21
    /**
22
     * Send a basic restore request for an archived copy of an object back into Amazon S3.
23
     * For a complete reference:
24
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/restore-object.html
25
     *
26
     * @param array $params
27
     *
28
     * @return bool
29
     * @throws Exception
30
     */
31 1
    public function handle(array $params = []): bool
32
    {
33 1
        $bucketName = $params[ 'bucket' ];
34 1
        $keyName    = $params[ 'key' ];
35 1
        $days       = (isset($params[ 'days' ])) ? $params[ 'days' ] : 5;
36 1
        $tier       = (isset($params[ 'tier' ])) ? $params[ 'tier' ] : 'Expedited';
37
38 1
        if ($this->client->hasEncoder()) {
39 1
            $keyName = $this->client->getEncoder()->encode($keyName);
40
        }
41
42 1
        $allowedTiers = [
43 1
                'Bulk',
44 1
                'Expedited',
45 1
                'Standard',
46 1
        ];
47
48 1
        if ($tier and !in_array($tier, $allowedTiers)) {
49
            throw new InvalidArgumentException(sprintf('%s is not a valid tier value. Allowed values are: [' . implode(',', $allowedTiers) . ']', $tier));
50
        }
51
52
        try {
53 1
            $request = $this->client->getConn()->restoreObject([
54 1
                    'Bucket'         => $bucketName,
55 1
                    'Key'            => $keyName,
56 1
                    'RestoreRequest' => [
57 1
                            'Days'                 => $days,
58 1
                            'GlacierJobParameters' => [
59 1
                                    'Tier' => $tier,
60 1
                            ],
61 1
                    ],
62 1
            ]);
63
64 1
            if (($request instanceof ResultInterface) and $request[ '@metadata' ][ 'statusCode' ] === 202) {
65 1
                $this->commandHandlerLogger?->log($this, sprintf('A request for restore \'%s\' item in \'%s\' bucket was successfully sended', $keyName, $bucketName));
66
67 1
                if ($this->client->hasCache()) {
68 1
                    $this->client->getCache()->set($bucketName, $keyName, '');
69
                }
70
71 1
                return true;
72
            }
73
74
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong during sending restore questo for \'%s\' item in \'%s\' bucket', $keyName, $bucketName), 'warning');
75
76
            return false;
77
        } catch (Exception $e) {
78
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
79
80
            throw $e;
81
        }
82
    }
83
84
    /**
85
     * @param array $params
86
     *
87
     * @return bool
88
     */
89 1
    public function validateParams(array $params = []): bool
90
    {
91 1
        return (
92 1
                isset($params[ 'bucket' ]) and
93 1
                isset($params[ 'key' ])
94 1
        );
95
    }
96
}
97