Test Failed
Branch master (3a0aa4)
by Domenico
16:55
created

DeleteItem::handle()   B

Complexity

Conditions 8
Paths 76

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 39
ccs 0
cts 30
cp 0
rs 8.4444
c 0
b 0
f 0
cc 8
nc 76
nop 1
crap 72
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 Aws\S3\Exception\S3Exception;
16
use Exception;
17
use Matecat\SimpleS3\Commands\CommandHandler;
18
19
class DeleteItem extends CommandHandler
20
{
21
    /**
22
     * Delete an item.
23
     * For a complete reference:
24
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-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
        $version    = (isset($params[ 'version' ])) ? $params[ 'version' ] : null;
36
37
        if ($this->client->hasEncoder()) {
38
            $keyName = $this->client->getEncoder()->encode($keyName);
39
        }
40
41
        try {
42
            $config = [
43
                    'Bucket' => $bucketName,
44
                    'Key'    => $keyName,
45
            ];
46
47
            if (null != $version) {
48
                $config[ 'VersionId' ] = $version;
49
            }
50
51
            $delete = $this->client->getConn()->deleteObject($config);
52
53
            if (($delete instanceof ResultInterface) and $delete[ '@metadata' ][ 'statusCode' ] === 204) {
54
                $this->commandHandlerLogger?->log($this, sprintf('File \'%s\' was successfully deleted from \'%s\' bucket', $keyName, $bucketName));
55
56
                if ($this->client->hasCache()) {
57
                    $this->client->getCache()->remove($bucketName, $keyName, $version);
58
                }
59
60
                return true;
61
            }
62
63
            $this->commandHandlerLogger?->log($this, sprintf('Something went wrong in deleting file \'%s\' from \'%s\' bucket', $keyName, $bucketName), 'warning');
64
65
            return false;
66
        } catch (S3Exception $e) {
67
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
68
69
            throw $e;
70
        }
71
    }
72
73
    /**
74
     * @param array $params
75
     *
76
     * @return bool
77
     */
78
    public function validateParams(array $params = []): bool
79
    {
80
        return (
81
                isset($params[ 'bucket' ]) and
82
                isset($params[ 'key' ])
83
        );
84
    }
85
}
86