CopyFolder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 36
dl 0
loc 74
ccs 0
cts 35
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 52 10
A validateParams() 0 6 3
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 CopyFolder extends CommandHandler
18
{
19
    /**
20
     * @param array $params
21
     *
22
     * @return mixed
23
     * @throws \Exception
24
     */
25
    public function handle($params = [])
26
    {
27
        $targetBucketName = (isset($params['target_bucket'])) ? $params['target_bucket'] : $params['source_bucket'];
28
        $targetFolder = $params['target_folder'];
29
        $sourceBucketName = $params['source_bucket'];
30
        $sourceFolder = $params['source_folder'];
31
32
        try {
33
            $sourceItems = $this->client->getItemsInABucket([
34
                    'bucket' => $sourceBucketName,
35
                    'prefix' => $sourceFolder,
36
            ]);
37
38
            $success = true;
39
40
            foreach ($sourceItems as $sourceItem) {
41
                if (false === File::endsWith($sourceFolder, $this->client->getPrefixSeparator())) {
42
                    $sourceFolder = $sourceFolder . $this->client->getPrefixSeparator();
43
                }
44
45
                $targetKeyName = $targetFolder . $this->client->getPrefixSeparator() . str_replace($sourceFolder, "", $sourceItem);
46
47
                $copiedSourceItems = $this->client->copyItem([
48
                        'target_bucket' => $targetBucketName,
49
                        'target' => $targetKeyName,
50
                        'source_bucket' => $sourceBucketName,
51
                        'source' => $sourceItem,
52
                ]);
53
54
                if ($copiedSourceItems === false) {
55
                    $success = false;
56
                }
57
            }
58
59
            if (isset($params['delete_source']) and true === $params['delete_source']) {
60
                $deleteSource = $this->client->deleteFolder([
61
                        'bucket' => $sourceBucketName,
62
                        'prefix' => $sourceFolder,
63
                ]);
64
65
                if ($deleteSource === true) {
66
                    $success = false;
67
                }
68
            }
69
70
            return $success;
71
        } catch (\Exception $e) {
72
            if (null !== $this->commandHandlerLogger) {
73
                $this->commandHandlerLogger->logExceptionAndReturnFalse($e);
74
            }
75
76
            throw $e;
77
        }
78
    }
79
80
    /**
81
     * @param array $params
82
     *
83
     * @return bool
84
     */
85
    public function validateParams($params = [])
86
    {
87
        return (
88
                isset($params['target_folder']) and
89
                isset($params['source_bucket']) and
90
                isset($params['source_folder'])
91
        );
92
    }
93
}
94