ExpirationCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
A execute() 0 31 4
1
<?php
2
3
namespace TusPhp\Commands;
4
5
use TusPhp\Config;
6
use TusPhp\Cache\CacheFactory;
7
use TusPhp\Tus\Server as TusServer;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ExpirationCommand extends Command
14
{
15
    /** @var TusServer */
16
    protected $server;
17
18
    /**
19
     * {@inheritDoc}
20
     */
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('tus:expired')
25
            ->setDescription('Remove expired uploads.')
26
            ->setHelp('Deletes all expired uploads to free server resources. Values can be redis, file or apcu. Defaults to file.')
27
            ->addArgument(
28
                'cache-adapter',
29
                InputArgument::OPTIONAL,
30
                'Cache adapter to use: redis, file or apcu',
31
                'file'
32
            )
33
            ->addOption(
34
                'config',
35
                'c',
36
                InputArgument::OPTIONAL,
37
                'File to get config parameters from.'
38
            );
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $output->writeln([
47
            '<info>Cleaning server resources</info>',
48
            '<info>=========================</info>',
49
            '',
50
        ]);
51
52
        $config = $input->getOption('config');
53
54
        if ( ! empty($config)) {
55
            Config::set($config);
56
        }
57
58
        $cacheAdapter = $input->getArgument('cache-adapter') ?? 'file';
59
60
        $this->server = new TusServer(CacheFactory::make($cacheAdapter));
61
62
        $deleted = $this->server->handleExpiration();
63
64
        if (empty($deleted)) {
65
            $output->writeln('<comment>Nothing to delete.</comment>');
66
        } else {
67
            foreach ($deleted as $key => $item) {
68
                $output->writeln('<comment>' . ($key + 1) . ". Deleted {$item['name']} from " . \dirname($item['file_path']) . '</comment>');
69
            }
70
        }
71
72
        $output->writeln('');
73
        
74
        return 0;
75
    }
76
}
77