ItemUploadCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 9.7998
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 ItemUploadCommand 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:item:upload')
37
                ->setDescription('Upload an object into a bucket.')
38
                ->setHelp('This command allows you to upload an object onto a S3 bucket.')
39
                ->addArgument('bucket', InputArgument::REQUIRED, 'The name of the bucket')
40
                ->addArgument('key', InputArgument::REQUIRED, 'The desired keyname')
41
                ->addArgument('src', InputArgument::REQUIRED, 'The source file');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $bucket = $input->getArgument('bucket');
47
        $key    = $input->getArgument('key');
48
        $src    = $input->getArgument('src');
49
        $io     = new SymfonyStyle($input, $output);
50
51
        try {
52
            if (true === $this->s3Client->uploadItem(['bucket' => $bucket, 'key' => $key, 'source' => $src])) {
53
                $io->success('The item was successfully uploaded');
54
55
                return 0;
56
            } else {
57
                $io->error('There was an error in the upload of the item');
58
59
                return 1;
60
            }
61
        } catch (Exception $e) {
62
            $io->error($e->getMessage());
63
64
            return 1;
65
        }
66
    }
67
}
68