Test Failed
Branch master (3a0aa4)
by Domenico
03:05
created

RestoreItem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
eloc 34
dl 0
loc 75
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 50 10
A validateParams() 0 5 2
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
    public function handle(array $params = []): bool
32
    {
33
        $bucketName = $params[ 'bucket' ];
34
        $keyName    = $params[ 'key' ];
35
        $days       = (isset($params[ 'days' ])) ? $params[ 'days' ] : 5;
36
        $tier       = (isset($params[ 'tier' ])) ? $params[ 'tier' ] : 'Expedited';
37
38
        if ($this->client->hasEncoder()) {
39
            $keyName = $this->client->getEncoder()->encode($keyName);
40
        }
41
42
        $allowedTiers = [
43
                'Bulk',
44
                'Expedited',
45
                'Standard',
46
        ];
47
48
        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
            $request = $this->client->getConn()->restoreObject([
54
                    'Bucket'         => $bucketName,
55
                    'Key'            => $keyName,
56
                    'RestoreRequest' => [
57
                            'Days'                 => $days,
58
                            'GlacierJobParameters' => [
59
                                    'Tier' => $tier,
60
                            ],
61
                    ],
62
            ]);
63
64
            if (($request instanceof ResultInterface) and $request[ '@metadata' ][ 'statusCode' ] === 202) {
65
                $this->commandHandlerLogger?->log($this, sprintf('A request for restore \'%s\' item in \'%s\' bucket was successfully sended', $keyName, $bucketName));
66
67
                if ($this->client->hasCache()) {
68
                    $this->client->getCache()->set($bucketName, $keyName, '');
69
                }
70
71
                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
    public function validateParams(array $params = []): bool
90
    {
91
        return (
92
                isset($params[ 'bucket' ]) and
93
                isset($params[ 'key' ])
94
        );
95
    }
96
}
97