BucketClearCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 5
nop 2
crap 12
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 BucketClearCommand 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:bucket:clear')
37
                ->setDescription('Clears a bucket.')
38
                ->setHelp('This command clears a bucket on S3.')
39
                ->addArgument('bucket', InputArgument::REQUIRED, 'The name of the bucket');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output): int
43
    {
44
        $bucket = $input->getArgument('bucket');
45
        $io     = new SymfonyStyle($input, $output);
46
47
        try {
48
            if (true === $this->s3Client->clearBucket(['bucket' => $bucket])) {
49
                $io->success('The bucket was successfully cleared');
50
51
                return 0;
52
            } else {
53
                $io->error('There was an error in clearing bucket');
54
55
                return 1;
56
            }
57
        } catch (Exception $e) {
58
            $io->error($e->getMessage());
59
60
            return 1;
61
        }
62
    }
63
}
64