DeleteItem   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
eloc 29
dl 0
loc 70
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 5 2
C handle() 0 45 11
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 Matecat\SimpleS3\Commands\CommandHandler;
17
18
class DeleteItem extends CommandHandler
19
{
20
    /**
21
     * Delete an item.
22
     * For a complete reference:
23
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html
24
     *
25
     * @param mixed $params
26
     *
27
     * @return bool
28
     * @throws \Exception
29
     */
30
    public function handle($params = [])
31
    {
32
        $bucketName = $params['bucket'];
33
        $keyName = $params['key'];
34
        $version = (isset($params['version'])) ? $params['version'] : null;
35
36
        if ($this->client->hasEncoder()) {
37
            $keyName = $this->client->getEncoder()->encode($keyName);
38
        }
39
40
        try {
41
            $config = [
42
                'Bucket' => $bucketName,
43
                'Key'    => $keyName,
44
            ];
45
46
            if (null != $version) {
47
                $config['VersionId'] = $version;
48
            }
49
50
            $delete = $this->client->getConn()->deleteObject($config);
51
52
            if (($delete instanceof ResultInterface) and $delete['@metadata']['statusCode'] === 204) {
53
                if (null !== $this->commandHandlerLogger) {
54
                    $this->commandHandlerLogger->log($this, sprintf('File \'%s\' was successfully deleted from \'%s\' bucket', $keyName, $bucketName));
55
                }
56
57
                if ($this->client->hasCache()) {
58
                    $this->client->getCache()->remove($bucketName, $keyName, $version);
59
                }
60
61
                return true;
62
            }
63
64
            if (null !== $this->commandHandlerLogger) {
65
                $this->commandHandlerLogger->log($this, sprintf('Something went wrong in deleting file \'%s\' from \'%s\' bucket', $keyName, $bucketName), 'warning');
66
            }
67
68
            return false;
69
        } catch (S3Exception $e) {
70
            if (null !== $this->commandHandlerLogger) {
71
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
72
            }
73
74
            throw $e;
75
        }
76
    }
77
78
    /**
79
     * @param array $params
80
     *
81
     * @return bool
82
     */
83
    public function validateParams($params = [])
84
    {
85
        return (
86
            isset($params['bucket']) and
87
            isset($params['key'])
88
        );
89
    }
90
}
91