Passed
Branch master (0cf34c)
by Ankit
02:34
created

ExpirationCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 21 3
A configure() 0 10 1
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. Values can be redis or file. Defaults to file.')
25
            ->addArgument(
26
                'cache-adapter',
27
                InputArgument::OPTIONAL,
28
                'Cache adapter to use, redis or file. Optional, defaults to file based cache.'
29
            );
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $output->writeln([
38
            '<info>Cleaning server resources</info>',
39
            '<info>=========================</info>',
40
            '',
41
        ]);
42
43
        $this->server = new TusServer($input->getArgument('cache-adapter') ?? 'file');
44
45
        $deleted = $this->server->handleExpiration();
46
47
        if (empty($deleted)) {
48
            $output->writeln('<comment>Nothing to delete.</comment>');
49
        } else {
50
            foreach ($deleted as $key => $item) {
51
                $output->writeln('<comment>' . ($key + 1) . ". Deleted {$item['name']} from {$item['file_path']}</comment>");
52
            }
53
        }
54
55
        $output->writeln('');
56
    }
57
}
58