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

Transfer::transfer()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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