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

ExpirationCommand::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 2
nop 2
dl 0
loc 21
rs 9.3142
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. 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