Test Failed
Branch master (3a0aa4)
by Domenico
03:45
created

GetCurrentItemVersion   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 8
eloc 19
dl 0
loc 52
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

2 Methods

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