ItemDownloadCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 28
dl 0
loc 56
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
A execute() 0 25 3
A __construct() 0 5 1
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 ItemDownloadCommand 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 $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:item:download')
37
                ->setDescription('Download an object from a bucket.')
38
                ->setHelp('This command allows you to download an object from a S3 bucket.')
39
                ->addArgument('bucket', InputArgument::REQUIRED, 'The name of the bucket')
40
                ->addArgument('key', InputArgument::REQUIRED, 'The desired keyname')
41
                ->addArgument('save_as', InputArgument::OPTIONAL, 'How to save the file on your filesystem');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        $bucket = $input->getArgument('bucket');
47
        $key    = $input->getArgument('key');
48
        $saveAs = $input->getArgument('save_as');
49
        $io     = new SymfonyStyle($input, $output);
50
51
        try {
52
            if (true === $this->s3Client->downloadItem([
53
                            'bucket'  => $bucket,
54
                            'key'     => $key,
55
                            'save_as' => $saveAs
56
                    ])) {
57
                $io->success('The item was successfully downloaded into [' . $saveAs . ']');
58
59
                return 0;
60
            } else {
61
                $io->error('There was an error in the download of the item');
62
63
                return 1;
64
            }
65
        } catch (Exception $e) {
66
            $io->error($e->getMessage());
67
68
            return 1;
69
        }
70
    }
71
}
72