RestoreItem::validateParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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