Completed
Pull Request — master (#4)
by Ankit
02:16
created

ExpirationCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp\Commands;
4
5
use TusPhp\Tus\Server as TusServer;
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
11
class ExpirationCommand extends Command
12
{
13
    /** @var TusServer */
14
    protected $server;
15
16
    /**
17
     * {@inheritDoc}
18
     */
19
    protected function configure()
20
    {
21
        $this
22
            ->setName('tus:expired')
23
            ->setDescription('Remove expired uploads.')
24
            ->setHelp('Deletes all expired uploads to free server resources.')
25
            ->addArgument('cache-adapter', InputArgument::OPTIONAL, 'Cache adapter to use.');
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $output->writeln([
34
            '<info>Cleaning server resources</info>',
35
            '<info>=========================</info>',
36
            '',
37
        ]);
38
39
        $this->server = new TusServer($input->getArgument('cache-adapter') ?? 'file');
40
41
        $deleted = $this->server->handleExpiration();
42
43
        if (empty($deleted)) {
44
            $output->writeln('<comment>Nothing to delete.</comment>');
45
        } else {
46
            foreach ($deleted as $key => $item) {
47
                $output->writeln('<comment>' . ($key + 1) . ". Deleted {$item['name']} from {$item['file_path']}</comment>");
48
            }
49
        }
50
51
        $output->writeln('');
52
    }
53
}
54