Transfer::transfer()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 10
c 0
b 0
f 0
cc 2
nc 4
nop 3
crap 2.2109
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 RuntimeException;
17
18
class Transfer extends CommandHandler
19
{
20
    /**
21
     * @param array $params
22
     *
23
     * @return bool
24
     * @throws Exception
25
     */
26 1
    public function handle(array $params = []): bool
27
    {
28 1
        $dest    = $params[ 'dest' ];
29 1
        $source  = $params[ 'source' ];
30 1
        $options = (isset($params[ 'options' ])) ? $params[ 'options' ] : [];
31
32 1
        return $this->transfer($dest, $source, $options);
33
    }
34
35
    /**
36
     * @param array $params
37
     *
38
     * @return bool
39
     */
40 1
    public function validateParams(array $params = []): bool
41
    {
42 1
        return (
43 1
                isset($params[ 'dest' ]) and
44 1
                isset($params[ 'source' ])
45 1
        );
46
    }
47
48
    /**
49
     * @param string $dest
50
     * @param string $source
51
     * @param array  $options
52
     *
53
     * @return bool
54
     * @throws Exception
55
     */
56 1
    private function transfer(string $dest, string $source, array $options = []): bool
57
    {
58
        try {
59 1
            $manager = new \Aws\S3\Transfer($this->client->getConn(), $source, $dest, $options);
60 1
            $manager->transfer();
61
62 1
            $this->commandHandlerLogger?->log($this, sprintf('Files were successfully transfered from \'%s\' to \'%s\'', $source, $dest));
63
64 1
            return true;
65
        } catch (RuntimeException $e) {
66
            $this->commandHandlerLogger?->logExceptionAndReturnFalse($e);
67
68
            throw $e;
69
        }
70
    }
71
}
72