Passed
Pull Request — master (#2)
by mon
03:19
created

UpdateCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 21
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12
1
<?php
2
3
namespace FileEye\MimeMap\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use FileEye\MimeMap\MapUpdater;
10
use FileEye\MimeMap\TypeExtensionMap;
11
12
/**
13
 * A Symfony application command to update the MIME type to extension map.
14
 */
15
class UpdateCommand extends Command
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('update')
24
            ->setDescription('Updates the MIME-type-to-extension map.')
25
            ->addArgument(
26
                'source-url',
27
                InputArgument::OPTIONAL,
28
                'URL of the source map',
29
                MapUpdater::DEFAULT_URL
30
            )
31
            ->addArgument(
32
                'output-file',
33
                InputArgument::OPTIONAL,
34
                'Path to the directory of the mapper class PHP file',
35
                MapUpdater::getDefaultCodeFilePath()
36
            )
37
        ;
38
    }
39
40
    // xx which MIME type to choose if an extension has several
41
    /*$duplicateResolution = [
42
        'sub' => 'text/vnd.dvb.subtitle',
43
        'wmz' => 'application/x-msmetafile',
44
    ];*/
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $updater = new MapUpdater();
52
        try {
53
            $new_map = $updater->loadMapFromUrl($input->getArgument('source-url'));
54
        } catch (\RuntimeException $e) {
55
            $output->writeln('<error>' . $e->getMessage() . '</error>');
56
            exit(2);
57
        }
58
        $current_map = (new TypeExtensionMap())->get();
59
        try {
60
            $updater->compareMaps($current_map, $new_map);
61
            $output->writeln('<info>No changes to mapping.</info>');
62
            exit(0);
63
        } catch (\RuntimeException $e) {
64
            $output->writeln('Changes to MIME types mapping:');
65
            $output->writeln($e->getMessage());
66
        }
67
        $updater->writeMapToCodeFile($new_map, $input->getArgument('output-file'));
68
        $output->writeln('<comment>Code updated.</comment>');
69
    }
70
}
71