DeleteItem   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
wmc 10
eloc 26
dl 0
loc 64
ccs 22
cts 27
cp 0.8148
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 5 2
B handle() 0 39 8
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 5
    public function handle(array $params = []): bool
32
    {
33 5
        $bucketName = $params[ 'bucket' ];
34 5
        $keyName    = $params[ 'key' ];
35 5
        $version    = (isset($params[ 'version' ])) ? $params[ 'version' ] : null;
36
37 5
        if ($this->client->hasEncoder()) {
38 4
            $keyName = $this->client->getEncoder()->encode($keyName);
39
        }
40
41
        try {
42 5
            $config = [
43 5
                    'Bucket' => $bucketName,
44 5
                    'Key'    => $keyName,
45 5
            ];
46
47 5
            if (null != $version) {
48 1
                $config[ 'VersionId' ] = $version;
49
            }
50
51 5
            $delete = $this->client->getConn()->deleteObject($config);
52
53 5
            if (($delete instanceof ResultInterface) and $delete[ '@metadata' ][ 'statusCode' ] === 204) {
54 5
                $this->commandHandlerLogger?->log($this, sprintf('File \'%s\' was successfully deleted from \'%s\' bucket', $keyName, $bucketName));
55
56 5
                if ($this->client->hasCache()) {
57 4
                    $this->client->getCache()->remove($bucketName, $keyName, $version);
58
                }
59
60 5
                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 5
    public function validateParams(array $params = []): bool
79
    {
80 5
        return (
81 5
                isset($params[ 'bucket' ]) and
82 5
                isset($params[ 'key' ])
83 5
        );
84
    }
85
}
86