CopyFolder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 35
dl 0
loc 78
ccs 28
cts 38
cp 0.7368
rs 10
c 1
b 0
f 0

2 Methods

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