FolderCopyCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Matecat\SimpleS3\Console;
4
5
use Exception;
6
use Matecat\SimpleS3\Client;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class FolderCopyCommand extends Command
14
{
15
    /**
16
     * @var Client
17
     */
18
    private Client $s3Client;
19
20
    /**
21
     * CacheFlushCommand constructor.
22
     *
23
     * @param Client      $s3Client
24
     * @param string|null $name
25
     */
26
    public function __construct(Client $s3Client, ?string $name = null)
27
    {
28
        parent::__construct($name);
29
30
        $this->s3Client = $s3Client;
31
    }
32
33
    protected function configure(): void
34
    {
35
        $this
36
                ->setName('ss3:folder:copy')
37
                ->setDescription('Copy the items from a folder to another one.')
38
                ->setHelp('This command allows you to copy items from a folder to another one.')
39
                ->addArgument('source_bucket', InputArgument::REQUIRED, 'The source bucket')
40
                ->addArgument('source_folder', InputArgument::REQUIRED, 'The source folder')
41
                ->addArgument('target_bucket', InputArgument::REQUIRED, 'The target bucket')
42
                ->addArgument('target_folder', InputArgument::REQUIRED, 'The target folder');
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $sourceBucket = $input->getArgument('source_bucket');
48
        $sourceFolder = $input->getArgument('source_folder');
49
        $targetBucket = $input->getArgument('target_bucket');
50
        $targetFolder = $input->getArgument('target_folder');
51
        $io           = new SymfonyStyle($input, $output);
52
53
        try {
54
            if (true === $this->s3Client->copyFolder([
55
                            'source_bucket' => $sourceBucket,
56
                            'source_folder' => $sourceFolder,
57
                            'target_bucket' => $targetBucket,
58
                            'target_folder' => $targetFolder,
59
                    ])) {
60
                $io->success('The items were successfully copied');
61
62
                return 0;
63
            } else {
64
                $io->error('There was an error during copying the items');
65
66
                return 1;
67
            }
68
        } catch (Exception $e) {
69
            $io->error($e->getMessage());
70
71
            return 1;
72
        }
73
    }
74
}
75