ItemDeleteCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
ccs 0
cts 12
cp 0
rs 9.8333
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 ItemDeleteCommand 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:delete')
37
                ->setDescription('Deletes an object from a bucket.')
38
                ->setHelp('This command allows you to delete 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
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output): int
44
    {
45
        $bucket = $input->getArgument('bucket');
46
        $key    = $input->getArgument('key');
47
        $io     = new SymfonyStyle($input, $output);
48
49
        try {
50
            if (true === $this->s3Client->deleteItem(['bucket' => $bucket, 'key' => $key])) {
51
                $io->success('The item was successfully cleared');
52
53
                return 0;
54
            } else {
55
                $io->error('There was an error in deleting the item');
56
57
                return 1;
58
            }
59
        } catch (Exception $e) {
60
            $io->error($e->getMessage());
61
62
            return 1;
63
        }
64
    }
65
}
66