Test Failed
Push — master ( a92e12...446184 )
by Domenico
04:27
created

GetCurrentItemVersion   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 50
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParams() 0 5 2
A handle() 0 24 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 Matecat\SimpleS3\Commands\CommandHandler;
15
use Matecat\SimpleS3\Helpers\File;
16
17
class GetCurrentItemVersion extends CommandHandler
18
{
19
    /**
20
     * Get the current version of an item.
21
     * For a complete reference:
22
     * https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-versioning.html?highlight=versioning%20bucket
23
     *
24
     * @param array $params
25
     *
26
     * @return null|string
27
     * @throws \Exception
28
     */
29 2
    public function handle($params = [])
30
    {
31 2
        $bucketName = $params['bucket'];
32 2
        $keyName = $params['key'];
33
34 2
        $fileInfo = File::getPathInfo($keyName);
35 2
        $prefix = $fileInfo['dirname'] . $this->client->getPrefixSeparator();
36
37 2
        $results = $this->client->getConn()->listObjectVersions([
38 2
            'Bucket' => $bucketName,
39 2
            'Prefix' => $prefix
40
        ]);
41
42 2
        if (false === isset($results['Versions'])) {
43
            return null;
44
        }
45
46 2
        if ($this->client->hasEncoder()) {
47 2
            $keyName = $this->client->getEncoder()->encode($keyName);
48
        }
49
50 2
        foreach ($results['Versions'] as $result) {
51 2
            if (true === $result['IsLatest'] and $keyName === $result['Key']) {
52 2
                return $result['VersionId'];
53
            }
54
        }
55
    }
56
57
    /**
58
     * @param array $params
59
     *
60
     * @return bool
61
     */
62 2
    public function validateParams($params = [])
63
    {
64
        return (
65 2
            isset($params['bucket']) and
66 2
            isset($params['key'])
67
        );
68
    }
69
}
70