OpenItem::validateParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 OpenItem extends CommandHandler
19
{
20
    /**
21
     * Open an item and return the content of it.
22
     *
23
     * @param array $params
24
     *
25
     * @return string|null
26
     * @throws Exception
27
     */
28 2
    public function handle(array $params = []): ?string
29
    {
30 2
        $bucketName = $params[ 'bucket' ];
31 2
        $keyName    = $params[ 'key' ];
32
33
        try {
34 2
            $url     = $this->client->getPublicItemLink(['bucket' => $bucketName, 'key' => $keyName]);
35 2
            $content = File::loadFile($url, $this->client->hasSslVerify());
36
37 2
            if (false === $content) {
38
                $this->commandHandlerLogger?->log($this, sprintf('Something went wrong during getting content of \'%s\' item from \'%s\' bucket', $keyName, $bucketName), 'warning');
39
40
                return null;
41
            }
42
43 2
            $this->commandHandlerLogger?->log($this, sprintf('Content from \'%s\' item was successfully obtained from \'%s\' bucket', $keyName, $bucketName));
44
45 2
            return $content;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $content could return the type true which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
46
        } catch (Exception $e) {
47
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
48
49
            throw $e;
50
        }
51
    }
52
53
    /**
54
     * @param array $params
55
     *
56
     * @return bool
57
     */
58 2
    public function validateParams(array $params = []): bool
59
    {
60 2
        return (
61 2
                isset($params[ 'bucket' ]) and
62 2
                isset($params[ 'key' ])
63 2
        );
64
    }
65
}
66