Passed
Push — master ( 77859c...e0031c )
by Mauro
02:25 queued 18s
created

HasFolder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 72
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 5
A validateParams() 0 5 2
A returnItemFromS3() 0 20 4
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\S3\Exception\S3Exception;
15
use Matecat\SimpleS3\Commands\CommandHandler;
16
use Matecat\SimpleS3\Helpers\File;
17
18
class HasFolder extends CommandHandler
19
{
20
    /**
21
     * Check if a folder already exists.
22
     * For a complete reference:
23
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/head-object.html
24
     *
25
     * @param array $params
26
     *
27
     * @return bool
28
     * @throws \Exception
29
     */
30
    public function handle($params = [])
31
    {
32
        $bucketName = $params['bucket'];
33
        $prefix = $params['prefix'];
34
35
        if ($this->client->hasEncoder()) {
36
            $prefix = $this->client->getEncoder()->encode($prefix);
37
        }
38
39
        if (false === File::endsWith($prefix, $this->client->getPrefixSeparator())) {
40
            $prefix .= $this->client->getPrefixSeparator();
41
        }
42
43
        if ($this->client->hasCache() and $this->client->getCache()->has($bucketName, $prefix)) {
44
            return true;
45
        }
46
47
        return $this->returnItemFromS3($bucketName, $prefix);
48
    }
49
50
    /**
51
     * @param array $params
52
     *
53
     * @return bool
54
     */
55
    public function validateParams($params = [])
56
    {
57
        return (
58
            isset($params['bucket']) and
59
            isset($params['prefix'])
60
        );
61
    }
62
63
    /**
64
     * @param string $bucketName
65
     * @param string $prefix
66
     *
67
     * @return bool
68
     * @throws \Exception
69
     */
70
    private function returnItemFromS3($bucketName, $prefix)
71
    {
72
        $command = $this->client->getConn()->getCommand(
73
            'listObjects',
74
            [
75
                'Bucket' => $bucketName,
76
                'Prefix' => $prefix,
77
                'MaxKeys' => 1,
78
            ]
79
        );
80
        try {
81
            $result = $this->client->getConn()->execute($command);
82
83
            return $result['Contents'] or $result['CommonPrefixes'];
84
        } catch (S3Exception $e) {
85
            if (null !== $this->commandHandlerLogger) {
86
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
87
            }
88
89
            throw $e;
90
        }
91
    }
92
}
93