Issues (45)

src/Console/ItemCopyCommand.php (1 issue)

1
<?php
2
3
namespace Matecat\SimpleS3\Console;
4
5
use Matecat\SimpleS3\Client;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12
class ItemCopyCommand extends Command
13
{
14
    /**
15
     * @var Client
16
     */
17
    private $s3Client;
18
19
    /**
20
     * CacheFlushCommand constructor.
21
     *
22
     * @param Client $s3Client
23
     * @param null   $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
24
     */
25
    public function __construct(Client $s3Client, $name = null)
26
    {
27
        parent::__construct($name);
28
29
        $this->s3Client = $s3Client;
30
    }
31
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('ss3:item:copy')
36
            ->setDescription('Copy an object from a bucket to another one.')
37
            ->setHelp('This command allows you to copy an object from a S3 bucket to another one.')
38
            ->addArgument('source_bucket', InputArgument::REQUIRED, 'The source bucket')
39
            ->addArgument('source_key', InputArgument::REQUIRED, 'The source keyname')
40
            ->addArgument('target_bucket', InputArgument::REQUIRED, 'The target bucket')
41
            ->addArgument('target_key', InputArgument::REQUIRED, 'The target keyname')
42
        ;
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $sourceBucket = $input->getArgument('source_bucket');
48
        $sourceKey = $input->getArgument('source_key');
49
        $targetBucket = $input->getArgument('target_bucket');
50
        $targetKey = $input->getArgument('target_key');
51
        $io = new SymfonyStyle($input, $output);
52
53
        try {
54
            if (true === $this->s3Client->copyItem([
55
                'source_bucket' => $sourceBucket,
56
                'source' => $sourceKey,
57
                'target_bucket' => $targetBucket,
58
                'target' => $targetKey,
59
            ])) {
60
                $io->success('The item was successfully copied');
61
            } else {
62
                $io->error('There was an error during copying the item');
63
            }
64
        } catch (\Exception $e) {
65
            $io->error($e->getMessage());
66
        }
67
    }
68
}
69